xref: /freebsd/sys/kern/kern_time.c (revision e96c1fdc3ff516d0042722c3918edb9a9be874bd)
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
34e96c1fdcSPeter Wemm  * $Id: kern_time.c,v 1.65 1999/04/27 11:16:07 phk Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38e96c1fdcSPeter Wemm #include <sys/systm.h>
39a5c9bce7SBruce Evans #include <sys/buf.h>
40d2d3e875SBruce Evans #include <sys/sysproto.h>
41df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
42797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
4594c8fcd8SPeter Wemm #include <sys/sysent.h>
46df8bae1dSRodney W. Grimes #include <sys/proc.h>
47708e7684SPeter Wemm #include <sys/time.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
495b870b7bSPeter Wemm #include <vm/vm.h>
505b870b7bSPeter Wemm #include <vm/vm_extern.h>
51df8bae1dSRodney W. Grimes 
52ac7e6123SDavid Greenman struct timezone tz;
53ac7e6123SDavid Greenman 
54df8bae1dSRodney W. Grimes /*
55df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
58df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
59df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
60df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
61df8bae1dSRodney W. Grimes  * timers when they expire.
62df8bae1dSRodney W. Grimes  */
63df8bae1dSRodney W. Grimes 
645b870b7bSPeter Wemm static int	nanosleep1 __P((struct proc *p, struct timespec *rqt,
655b870b7bSPeter Wemm 		    struct timespec *rmt));
667d7fb492SBruce Evans static int	settime __P((struct timeval *));
677d7fb492SBruce Evans static void	timevalfix __P((struct timeval *));
68cb226aaaSPoul-Henning Kamp static void	no_lease_updatetime __P((int));
6994c8fcd8SPeter Wemm 
701b09ae77SPoul-Henning Kamp static void
711b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
721b09ae77SPoul-Henning Kamp 	int deltat;
731b09ae77SPoul-Henning Kamp {
741b09ae77SPoul-Henning Kamp }
751b09ae77SPoul-Henning Kamp 
761b09ae77SPoul-Henning Kamp void (*lease_updatetime) __P((int))  = no_lease_updatetime;
771b09ae77SPoul-Henning Kamp 
7894c8fcd8SPeter Wemm static int
7994c8fcd8SPeter Wemm settime(tv)
8094c8fcd8SPeter Wemm 	struct timeval *tv;
8194c8fcd8SPeter Wemm {
82fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
83c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
847ec73f64SPoul-Henning Kamp 	struct timespec ts;
8594c8fcd8SPeter Wemm 	int s;
8694c8fcd8SPeter Wemm 
87708e7684SPeter Wemm 	s = splclock();
889c8fff87SBruce Evans 	microtime(&tv1);
8900af9731SPoul-Henning Kamp 	delta = *tv;
9000af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9194c8fcd8SPeter Wemm 
9294c8fcd8SPeter Wemm 	/*
939c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
94fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
95fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
96fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
97fcae3aa6SNick Sayer 	 * back to the past.
98c0bd94a7SNick Sayer 	 *
99c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
100c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
101c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
10294c8fcd8SPeter Wemm 	 */
103fcae3aa6SNick Sayer 	if (securelevel > 1) {
104fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1053f92429aSMatt Jacob 			/*
106c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1073f92429aSMatt Jacob 			 */
108fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
109fcae3aa6SNick Sayer 				maxtime = tv1;
110fcae3aa6SNick Sayer 			tv2 = *tv;
111fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
112fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1133f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
114fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
115fcae3aa6SNick Sayer 			}
1163f92429aSMatt Jacob 		} else {
117c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
118c0bd94a7SNick Sayer 				splx(s);
119c0bd94a7SNick Sayer 				return (EPERM);
120c0bd94a7SNick Sayer 			}
121c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
122c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
123c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
124c0bd94a7SNick Sayer 			}
125c0bd94a7SNick Sayer 			laststep = *tv;
126fcae3aa6SNick Sayer 		}
1279c8fff87SBruce Evans 	}
1289c8fff87SBruce Evans 
1297ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1307ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1317ec73f64SPoul-Henning Kamp 	set_timecounter(&ts);
13294c8fcd8SPeter Wemm 	(void) splsoftclock();
13394c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
13494c8fcd8SPeter Wemm 	splx(s);
13594c8fcd8SPeter Wemm 	resettodr();
13694c8fcd8SPeter Wemm 	return (0);
13794c8fcd8SPeter Wemm }
13894c8fcd8SPeter Wemm 
13994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
14094c8fcd8SPeter Wemm struct clock_gettime_args {
14194c8fcd8SPeter Wemm 	clockid_t clock_id;
14294c8fcd8SPeter Wemm 	struct	timespec *tp;
14394c8fcd8SPeter Wemm };
14494c8fcd8SPeter Wemm #endif
145708e7684SPeter Wemm 
14694c8fcd8SPeter Wemm /* ARGSUSED */
14794c8fcd8SPeter Wemm int
148cb226aaaSPoul-Henning Kamp clock_gettime(p, uap)
14994c8fcd8SPeter Wemm 	struct proc *p;
15094c8fcd8SPeter Wemm 	struct clock_gettime_args *uap;
15194c8fcd8SPeter Wemm {
15294c8fcd8SPeter Wemm 	struct timespec ats;
15394c8fcd8SPeter Wemm 
154708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
15594c8fcd8SPeter Wemm 		return (EINVAL);
1567ec73f64SPoul-Henning Kamp 	nanotime(&ats);
157708e7684SPeter Wemm 	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
15894c8fcd8SPeter Wemm }
15994c8fcd8SPeter Wemm 
16094c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
16194c8fcd8SPeter Wemm struct clock_settime_args {
16294c8fcd8SPeter Wemm 	clockid_t clock_id;
16394c8fcd8SPeter Wemm 	const struct	timespec *tp;
16494c8fcd8SPeter Wemm };
16594c8fcd8SPeter Wemm #endif
166708e7684SPeter Wemm 
16794c8fcd8SPeter Wemm /* ARGSUSED */
16894c8fcd8SPeter Wemm int
169cb226aaaSPoul-Henning Kamp clock_settime(p, uap)
17094c8fcd8SPeter Wemm 	struct proc *p;
17194c8fcd8SPeter Wemm 	struct clock_settime_args *uap;
17294c8fcd8SPeter Wemm {
17394c8fcd8SPeter Wemm 	struct timeval atv;
17494c8fcd8SPeter Wemm 	struct timespec ats;
17594c8fcd8SPeter Wemm 	int error;
17694c8fcd8SPeter Wemm 
177f711d546SPoul-Henning Kamp 	if ((error = suser(p)) != 0)
17894c8fcd8SPeter Wemm 		return (error);
179708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
18094c8fcd8SPeter Wemm 		return (EINVAL);
18194c8fcd8SPeter Wemm 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
18294c8fcd8SPeter Wemm 		return (error);
1833fc9295dSBruce Evans 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
184708e7684SPeter Wemm 		return (EINVAL);
185a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
18694c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
18794c8fcd8SPeter Wemm 	if ((error = settime(&atv)))
18894c8fcd8SPeter Wemm 		return (error);
189708e7684SPeter Wemm 	return (0);
19094c8fcd8SPeter Wemm }
19194c8fcd8SPeter Wemm 
19294c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
19394c8fcd8SPeter Wemm struct clock_getres_args {
19494c8fcd8SPeter Wemm 	clockid_t clock_id;
19594c8fcd8SPeter Wemm 	struct	timespec *tp;
19694c8fcd8SPeter Wemm };
19794c8fcd8SPeter Wemm #endif
198708e7684SPeter Wemm 
19994c8fcd8SPeter Wemm int
200cb226aaaSPoul-Henning Kamp clock_getres(p, uap)
20194c8fcd8SPeter Wemm 	struct proc *p;
20294c8fcd8SPeter Wemm 	struct clock_getres_args *uap;
20394c8fcd8SPeter Wemm {
20494c8fcd8SPeter Wemm 	struct timespec ts;
205708e7684SPeter Wemm 	int error;
20694c8fcd8SPeter Wemm 
207708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
20894c8fcd8SPeter Wemm 		return (EINVAL);
209708e7684SPeter Wemm 	error = 0;
21094c8fcd8SPeter Wemm 	if (SCARG(uap, tp)) {
21194c8fcd8SPeter Wemm 		ts.tv_sec = 0;
212a58f0f8eSPoul-Henning Kamp 		ts.tv_nsec = 1000000000 / timecounter->tc_frequency;
21394c8fcd8SPeter Wemm 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
21494c8fcd8SPeter Wemm 	}
215708e7684SPeter Wemm 	return (error);
21694c8fcd8SPeter Wemm }
21794c8fcd8SPeter Wemm 
21894c8fcd8SPeter Wemm static int nanowait;
21994c8fcd8SPeter Wemm 
2205b870b7bSPeter Wemm static int
2215b870b7bSPeter Wemm nanosleep1(p, rqt, rmt)
2225b870b7bSPeter Wemm 	struct proc *p;
2235b870b7bSPeter Wemm 	struct timespec *rqt, *rmt;
2245b870b7bSPeter Wemm {
2255704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
22633841826SPoul-Henning Kamp 	struct timeval tv;
22733841826SPoul-Henning Kamp 	int error;
2285b870b7bSPeter Wemm 
2297d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
230708e7684SPeter Wemm 		return (EINVAL);
231d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2327d7fb492SBruce Evans 		return (0);
233c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
23400af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
23533841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
23633841826SPoul-Henning Kamp 	for (;;) {
23733841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
23833841826SPoul-Henning Kamp 		    tvtohz(&tv));
239c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
24033841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
24133841826SPoul-Henning Kamp 			if (error == ERESTART)
24294c8fcd8SPeter Wemm 				error = EINTR;
24333841826SPoul-Henning Kamp 			if (rmt != NULL) {
24433841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
24533841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
24633841826SPoul-Henning Kamp 					timespecclear(&ts);
24700af9731SPoul-Henning Kamp 				*rmt = ts;
24800af9731SPoul-Henning Kamp 			}
24933841826SPoul-Henning Kamp 			return (error);
25033841826SPoul-Henning Kamp 		}
25133841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
25233841826SPoul-Henning Kamp 			return (0);
2535704ba6aSPoul-Henning Kamp 		ts3 = ts;
2545704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2555704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
25633841826SPoul-Henning Kamp 	}
2575b870b7bSPeter Wemm }
25894c8fcd8SPeter Wemm 
2595b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2605b870b7bSPeter Wemm struct nanosleep_args {
2615b870b7bSPeter Wemm 	struct	timespec *rqtp;
2625b870b7bSPeter Wemm 	struct	timespec *rmtp;
2635b870b7bSPeter Wemm };
2645b870b7bSPeter Wemm #endif
2655b870b7bSPeter Wemm 
2665b870b7bSPeter Wemm /* ARGSUSED */
2675b870b7bSPeter Wemm int
268cb226aaaSPoul-Henning Kamp nanosleep(p, uap)
2695b870b7bSPeter Wemm 	struct proc *p;
2705b870b7bSPeter Wemm 	struct nanosleep_args *uap;
2715b870b7bSPeter Wemm {
2725b870b7bSPeter Wemm 	struct timespec rmt, rqt;
2735b870b7bSPeter Wemm 	int error, error2;
2745b870b7bSPeter Wemm 
2755b870b7bSPeter Wemm 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
2765b870b7bSPeter Wemm 	if (error)
2775b870b7bSPeter Wemm 		return (error);
278bf5acbf5SPeter Wemm 	if (SCARG(uap, rmtp))
2795b870b7bSPeter Wemm 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt), B_WRITE))
2805b870b7bSPeter Wemm 			return (EFAULT);
2815b870b7bSPeter Wemm 	error = nanosleep1(p, &rqt, &rmt);
282d59fbbf6SPeter Wemm 	if (error && SCARG(uap, rmtp)) {
283708e7684SPeter Wemm 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
2845b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
285708e7684SPeter Wemm 			return (error2);
286708e7684SPeter Wemm 	}
287708e7684SPeter Wemm 	return (error);
28894c8fcd8SPeter Wemm }
28994c8fcd8SPeter Wemm 
2905b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
291df8bae1dSRodney W. Grimes struct gettimeofday_args {
292df8bae1dSRodney W. Grimes 	struct	timeval *tp;
293df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
294df8bae1dSRodney W. Grimes };
295d2d3e875SBruce Evans #endif
296df8bae1dSRodney W. Grimes /* ARGSUSED */
29726f9a767SRodney W. Grimes int
298cb226aaaSPoul-Henning Kamp gettimeofday(p, uap)
299df8bae1dSRodney W. Grimes 	struct proc *p;
300df8bae1dSRodney W. Grimes 	register struct gettimeofday_args *uap;
301df8bae1dSRodney W. Grimes {
302df8bae1dSRodney W. Grimes 	struct timeval atv;
303df8bae1dSRodney W. Grimes 	int error = 0;
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	if (uap->tp) {
306df8bae1dSRodney W. Grimes 		microtime(&atv);
307bb56ec4aSPoul-Henning Kamp 		if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
308bb56ec4aSPoul-Henning Kamp 		    sizeof (atv))))
309df8bae1dSRodney W. Grimes 			return (error);
310df8bae1dSRodney W. Grimes 	}
311df8bae1dSRodney W. Grimes 	if (uap->tzp)
312df8bae1dSRodney W. Grimes 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
313df8bae1dSRodney W. Grimes 		    sizeof (tz));
314df8bae1dSRodney W. Grimes 	return (error);
315df8bae1dSRodney W. Grimes }
316df8bae1dSRodney W. Grimes 
317d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
318df8bae1dSRodney W. Grimes struct settimeofday_args {
319df8bae1dSRodney W. Grimes 	struct	timeval *tv;
320df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
321df8bae1dSRodney W. Grimes };
322d2d3e875SBruce Evans #endif
323df8bae1dSRodney W. Grimes /* ARGSUSED */
32426f9a767SRodney W. Grimes int
325cb226aaaSPoul-Henning Kamp settimeofday(p, uap)
326df8bae1dSRodney W. Grimes 	struct proc *p;
327df8bae1dSRodney W. Grimes 	struct settimeofday_args *uap;
328df8bae1dSRodney W. Grimes {
329708e7684SPeter Wemm 	struct timeval atv;
330df8bae1dSRodney W. Grimes 	struct timezone atz;
331708e7684SPeter Wemm 	int error;
332df8bae1dSRodney W. Grimes 
333f711d546SPoul-Henning Kamp 	if ((error = suser(p)))
334df8bae1dSRodney W. Grimes 		return (error);
335df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
336708e7684SPeter Wemm 	if (uap->tv) {
337708e7684SPeter Wemm 		if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
338708e7684SPeter Wemm 		    sizeof(atv))))
339df8bae1dSRodney W. Grimes 			return (error);
3400808c591SBruce Evans 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3410808c591SBruce Evans 			return (EINVAL);
342708e7684SPeter Wemm 	}
343df8bae1dSRodney W. Grimes 	if (uap->tzp &&
344df8bae1dSRodney W. Grimes 	    (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
345df8bae1dSRodney W. Grimes 		return (error);
34694c8fcd8SPeter Wemm 	if (uap->tv && (error = settime(&atv)))
34794c8fcd8SPeter Wemm 		return (error);
348df8bae1dSRodney W. Grimes 	if (uap->tzp)
349df8bae1dSRodney W. Grimes 		tz = atz;
350df8bae1dSRodney W. Grimes 	return (0);
351df8bae1dSRodney W. Grimes }
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes int	tickdelta;			/* current clock skew, us. per tick */
354df8bae1dSRodney W. Grimes long	timedelta;			/* unapplied time correction, us. */
35587b6de2bSPoul-Henning Kamp static long	bigadj = 1000000;	/* use 10x skew above bigadj us. */
356df8bae1dSRodney W. Grimes 
357d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
358df8bae1dSRodney W. Grimes struct adjtime_args {
359df8bae1dSRodney W. Grimes 	struct timeval *delta;
360df8bae1dSRodney W. Grimes 	struct timeval *olddelta;
361df8bae1dSRodney W. Grimes };
362d2d3e875SBruce Evans #endif
363df8bae1dSRodney W. Grimes /* ARGSUSED */
36426f9a767SRodney W. Grimes int
365cb226aaaSPoul-Henning Kamp adjtime(p, uap)
366df8bae1dSRodney W. Grimes 	struct proc *p;
367df8bae1dSRodney W. Grimes 	register struct adjtime_args *uap;
368df8bae1dSRodney W. Grimes {
369df8bae1dSRodney W. Grimes 	struct timeval atv;
370df8bae1dSRodney W. Grimes 	register long ndelta, ntickdelta, odelta;
371df8bae1dSRodney W. Grimes 	int s, error;
372df8bae1dSRodney W. Grimes 
373f711d546SPoul-Henning Kamp 	if ((error = suser(p)))
374df8bae1dSRodney W. Grimes 		return (error);
375bb56ec4aSPoul-Henning Kamp 	if ((error =
376bb56ec4aSPoul-Henning Kamp 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof(struct timeval))))
377df8bae1dSRodney W. Grimes 		return (error);
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes 	/*
380df8bae1dSRodney W. Grimes 	 * Compute the total correction and the rate at which to apply it.
381df8bae1dSRodney W. Grimes 	 * Round the adjustment down to a whole multiple of the per-tick
382df8bae1dSRodney W. Grimes 	 * delta, so that after some number of incremental changes in
383df8bae1dSRodney W. Grimes 	 * hardclock(), tickdelta will become zero, lest the correction
384df8bae1dSRodney W. Grimes 	 * overshoot and start taking us away from the desired final time.
385df8bae1dSRodney W. Grimes 	 */
386df8bae1dSRodney W. Grimes 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
3870cd97b20SBruce Evans 	if (ndelta > bigadj || ndelta < -bigadj)
388df8bae1dSRodney W. Grimes 		ntickdelta = 10 * tickadj;
389df8bae1dSRodney W. Grimes 	else
390df8bae1dSRodney W. Grimes 		ntickdelta = tickadj;
391df8bae1dSRodney W. Grimes 	if (ndelta % ntickdelta)
392df8bae1dSRodney W. Grimes 		ndelta = ndelta / ntickdelta * ntickdelta;
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 	/*
395df8bae1dSRodney W. Grimes 	 * To make hardclock()'s job easier, make the per-tick delta negative
396df8bae1dSRodney W. Grimes 	 * if we want time to run slower; then hardclock can simply compute
397df8bae1dSRodney W. Grimes 	 * tick + tickdelta, and subtract tickdelta from timedelta.
398df8bae1dSRodney W. Grimes 	 */
399df8bae1dSRodney W. Grimes 	if (ndelta < 0)
400df8bae1dSRodney W. Grimes 		ntickdelta = -ntickdelta;
401df8bae1dSRodney W. Grimes 	s = splclock();
402df8bae1dSRodney W. Grimes 	odelta = timedelta;
403df8bae1dSRodney W. Grimes 	timedelta = ndelta;
404df8bae1dSRodney W. Grimes 	tickdelta = ntickdelta;
405df8bae1dSRodney W. Grimes 	splx(s);
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes 	if (uap->olddelta) {
408df8bae1dSRodney W. Grimes 		atv.tv_sec = odelta / 1000000;
409df8bae1dSRodney W. Grimes 		atv.tv_usec = odelta % 1000000;
410df8bae1dSRodney W. Grimes 		(void) copyout((caddr_t)&atv, (caddr_t)uap->olddelta,
411df8bae1dSRodney W. Grimes 		    sizeof(struct timeval));
412df8bae1dSRodney W. Grimes 	}
413df8bae1dSRodney W. Grimes 	return (0);
414df8bae1dSRodney W. Grimes }
415df8bae1dSRodney W. Grimes 
416df8bae1dSRodney W. Grimes /*
417df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
418df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
419df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
420df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
421df8bae1dSRodney W. Grimes  *
422df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
423df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
424df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
425df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
426df8bae1dSRodney W. Grimes  *
427df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
428df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
429df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
430df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
431df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
432df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
433df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
434df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
435df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
436df8bae1dSRodney W. Grimes  */
437d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
438df8bae1dSRodney W. Grimes struct getitimer_args {
439df8bae1dSRodney W. Grimes 	u_int	which;
440df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
441df8bae1dSRodney W. Grimes };
442d2d3e875SBruce Evans #endif
443df8bae1dSRodney W. Grimes /* ARGSUSED */
44426f9a767SRodney W. Grimes int
445cb226aaaSPoul-Henning Kamp getitimer(p, uap)
446df8bae1dSRodney W. Grimes 	struct proc *p;
447df8bae1dSRodney W. Grimes 	register struct getitimer_args *uap;
448df8bae1dSRodney W. Grimes {
449227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
450df8bae1dSRodney W. Grimes 	struct itimerval aitv;
451df8bae1dSRodney W. Grimes 	int s;
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
454df8bae1dSRodney W. Grimes 		return (EINVAL);
455227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX still needed ? */
456df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
457df8bae1dSRodney W. Grimes 		/*
458ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
459df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
460df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
461df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
462df8bae1dSRodney W. Grimes 		 */
463df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
4644cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
465c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4664cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4674cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
468df8bae1dSRodney W. Grimes 			else
469227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
470227ee8a1SPoul-Henning Kamp 		}
471df8bae1dSRodney W. Grimes 	} else
472df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
473df8bae1dSRodney W. Grimes 	splx(s);
474df8bae1dSRodney W. Grimes 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
475df8bae1dSRodney W. Grimes 	    sizeof (struct itimerval)));
476df8bae1dSRodney W. Grimes }
477df8bae1dSRodney W. Grimes 
478d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
479df8bae1dSRodney W. Grimes struct setitimer_args {
480df8bae1dSRodney W. Grimes 	u_int	which;
481df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
482df8bae1dSRodney W. Grimes };
483d2d3e875SBruce Evans #endif
484df8bae1dSRodney W. Grimes /* ARGSUSED */
48526f9a767SRodney W. Grimes int
486cb226aaaSPoul-Henning Kamp setitimer(p, uap)
487df8bae1dSRodney W. Grimes 	struct proc *p;
488df8bae1dSRodney W. Grimes 	register struct setitimer_args *uap;
489df8bae1dSRodney W. Grimes {
490df8bae1dSRodney W. Grimes 	struct itimerval aitv;
491227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
492df8bae1dSRodney W. Grimes 	register struct itimerval *itvp;
493df8bae1dSRodney W. Grimes 	int s, error;
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
496df8bae1dSRodney W. Grimes 		return (EINVAL);
497df8bae1dSRodney W. Grimes 	itvp = uap->itv;
498df8bae1dSRodney W. Grimes 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
499df8bae1dSRodney W. Grimes 	    sizeof(struct itimerval))))
500df8bae1dSRodney W. Grimes 		return (error);
5010808c591SBruce Evans 	if ((uap->itv = uap->oitv) &&
502cb226aaaSPoul-Henning Kamp 	    (error = getitimer(p, (struct getitimer_args *)uap)))
503df8bae1dSRodney W. Grimes 		return (error);
504df8bae1dSRodney W. Grimes 	if (itvp == 0)
505df8bae1dSRodney W. Grimes 		return (0);
506cd9f713dSAndrey A. Chernov 	if (itimerfix(&aitv.it_value))
507cd9f713dSAndrey A. Chernov 		return (EINVAL);
5084cf41af3SPoul-Henning Kamp 	if (!timevalisset(&aitv.it_value))
5094cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
510cd9f713dSAndrey A. Chernov 	else if (itimerfix(&aitv.it_interval))
511df8bae1dSRodney W. Grimes 		return (EINVAL);
512227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX: still needed ? */
513df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
5144cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
515ab36c067SJustin T. Gibbs 			untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
5164cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value))
517ab36c067SJustin T. Gibbs 			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
51891ad39c6SPoul-Henning Kamp 						tvtohz(&aitv.it_value));
519c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
520bfe6c9faSPoul-Henning Kamp 		timevaladd(&aitv.it_value, &ctv);
521df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
522df8bae1dSRodney W. Grimes 	} else
523df8bae1dSRodney W. Grimes 		p->p_stats->p_timer[uap->which] = aitv;
524df8bae1dSRodney W. Grimes 	splx(s);
525df8bae1dSRodney W. Grimes 	return (0);
526df8bae1dSRodney W. Grimes }
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes /*
529df8bae1dSRodney W. Grimes  * Real interval timer expired:
530df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
531df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
532df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
533df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
534df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
535c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5369207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5379207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5389207f00aSBruce Evans  * interrupt even when we're delayed.
539df8bae1dSRodney W. Grimes  */
540df8bae1dSRodney W. Grimes void
541df8bae1dSRodney W. Grimes realitexpire(arg)
542df8bae1dSRodney W. Grimes 	void *arg;
543df8bae1dSRodney W. Grimes {
544df8bae1dSRodney W. Grimes 	register struct proc *p;
545bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
546df8bae1dSRodney W. Grimes 	int s;
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
549df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5504cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5514cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
552df8bae1dSRodney W. Grimes 		return;
553df8bae1dSRodney W. Grimes 	}
554df8bae1dSRodney W. Grimes 	for (;;) {
555227ee8a1SPoul-Henning Kamp 		s = splclock(); /* XXX: still neeeded ? */
556df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
557df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
558c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5594cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
560bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
561bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
562ee002b68SBruce Evans 			p->p_ithandle = timeout(realitexpire, (caddr_t)p,
563ee002b68SBruce Evans 			    tvtohz(&ntv) - 1);
564df8bae1dSRodney W. Grimes 			splx(s);
565df8bae1dSRodney W. Grimes 			return;
566df8bae1dSRodney W. Grimes 		}
567df8bae1dSRodney W. Grimes 		splx(s);
568df8bae1dSRodney W. Grimes 	}
569df8bae1dSRodney W. Grimes }
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes /*
572df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
573df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
574df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
575df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
576df8bae1dSRodney W. Grimes  */
57726f9a767SRodney W. Grimes int
578df8bae1dSRodney W. Grimes itimerfix(tv)
579df8bae1dSRodney W. Grimes 	struct timeval *tv;
580df8bae1dSRodney W. Grimes {
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
583df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
584df8bae1dSRodney W. Grimes 		return (EINVAL);
585df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
586df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
587df8bae1dSRodney W. Grimes 	return (0);
588df8bae1dSRodney W. Grimes }
589df8bae1dSRodney W. Grimes 
590df8bae1dSRodney W. Grimes /*
591df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
592df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
593df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
594df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
595df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
596df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
597df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
598df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
599df8bae1dSRodney W. Grimes  */
60026f9a767SRodney W. Grimes int
601df8bae1dSRodney W. Grimes itimerdecr(itp, usec)
602df8bae1dSRodney W. Grimes 	register struct itimerval *itp;
603df8bae1dSRodney W. Grimes 	int usec;
604df8bae1dSRodney W. Grimes {
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
607df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
608df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
609df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
610df8bae1dSRodney W. Grimes 			goto expire;
611df8bae1dSRodney W. Grimes 		}
612df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
613df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
614df8bae1dSRodney W. Grimes 	}
615df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
616df8bae1dSRodney W. Grimes 	usec = 0;
6174cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
618df8bae1dSRodney W. Grimes 		return (1);
619df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
620df8bae1dSRodney W. Grimes expire:
6214cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
622df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
623df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
624df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
625df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
626df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
627df8bae1dSRodney W. Grimes 		}
628df8bae1dSRodney W. Grimes 	} else
629df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
630df8bae1dSRodney W. Grimes 	return (0);
631df8bae1dSRodney W. Grimes }
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes /*
634df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
635df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
636df8bae1dSRodney W. Grimes  * results which are before the beginning,
637df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
638df8bae1dSRodney W. Grimes  * Caveat emptor.
639df8bae1dSRodney W. Grimes  */
64026f9a767SRodney W. Grimes void
641df8bae1dSRodney W. Grimes timevaladd(t1, t2)
642df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
643df8bae1dSRodney W. Grimes {
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
646df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
647df8bae1dSRodney W. Grimes 	timevalfix(t1);
648df8bae1dSRodney W. Grimes }
649df8bae1dSRodney W. Grimes 
65026f9a767SRodney W. Grimes void
651df8bae1dSRodney W. Grimes timevalsub(t1, t2)
652df8bae1dSRodney W. Grimes 	struct timeval *t1, *t2;
653df8bae1dSRodney W. Grimes {
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
656df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
657df8bae1dSRodney W. Grimes 	timevalfix(t1);
658df8bae1dSRodney W. Grimes }
659df8bae1dSRodney W. Grimes 
66087b6de2bSPoul-Henning Kamp static void
661df8bae1dSRodney W. Grimes timevalfix(t1)
662df8bae1dSRodney W. Grimes 	struct timeval *t1;
663df8bae1dSRodney W. Grimes {
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
666df8bae1dSRodney W. Grimes 		t1->tv_sec--;
667df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
668df8bae1dSRodney W. Grimes 	}
669df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
670df8bae1dSRodney W. Grimes 		t1->tv_sec++;
671df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
672df8bae1dSRodney W. Grimes 	}
673df8bae1dSRodney W. Grimes }
674