xref: /freebsd/sys/kern/kern_time.c (revision ac0653dcc8ca174f193f1f9f056d18e2c52f1b25)
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
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
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>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
4694c8fcd8SPeter Wemm #include <sys/sysent.h>
47df8bae1dSRodney W. Grimes #include <sys/proc.h>
48708e7684SPeter Wemm #include <sys/time.h>
4991266b96SPoul-Henning Kamp #include <sys/timetc.h>
50df8bae1dSRodney W. Grimes #include <sys/vnode.h>
51fb919e4dSMark Murray 
525b870b7bSPeter Wemm #include <vm/vm.h>
535b870b7bSPeter Wemm #include <vm/vm_extern.h>
54df8bae1dSRodney W. Grimes 
55ac7e6123SDavid Greenman struct timezone tz;
56ac7e6123SDavid Greenman 
57df8bae1dSRodney W. Grimes /*
58df8bae1dSRodney W. Grimes  * Time of day and interval timer support.
59df8bae1dSRodney W. Grimes  *
60df8bae1dSRodney W. Grimes  * These routines provide the kernel entry points to get and set
61df8bae1dSRodney W. Grimes  * the time-of-day and per-process interval timers.  Subroutines
62df8bae1dSRodney W. Grimes  * here provide support for adding and subtracting timeval structures
63df8bae1dSRodney W. Grimes  * and decrementing interval timers, optionally reloading the interval
64df8bae1dSRodney W. Grimes  * timers when they expire.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
674d77a549SAlfred Perlstein static int	nanosleep1(struct thread *td, struct timespec *rqt,
684d77a549SAlfred Perlstein 		    struct timespec *rmt);
697edfb592SJohn Baldwin static int	settime(struct thread *, struct timeval *);
704d77a549SAlfred Perlstein static void	timevalfix(struct timeval *);
714d77a549SAlfred Perlstein static void	no_lease_updatetime(int);
7294c8fcd8SPeter Wemm 
731b09ae77SPoul-Henning Kamp static void
741b09ae77SPoul-Henning Kamp no_lease_updatetime(deltat)
751b09ae77SPoul-Henning Kamp 	int deltat;
761b09ae77SPoul-Henning Kamp {
771b09ae77SPoul-Henning Kamp }
781b09ae77SPoul-Henning Kamp 
794d77a549SAlfred Perlstein void (*lease_updatetime)(int)  = no_lease_updatetime;
801b09ae77SPoul-Henning Kamp 
8194c8fcd8SPeter Wemm static int
8291afe087SPoul-Henning Kamp settime(struct thread *td, struct timeval *tv)
8394c8fcd8SPeter Wemm {
84fcae3aa6SNick Sayer 	struct timeval delta, tv1, tv2;
85c0bd94a7SNick Sayer 	static struct timeval maxtime, laststep;
867ec73f64SPoul-Henning Kamp 	struct timespec ts;
8794c8fcd8SPeter Wemm 	int s;
8894c8fcd8SPeter Wemm 
89708e7684SPeter Wemm 	s = splclock();
909c8fff87SBruce Evans 	microtime(&tv1);
9100af9731SPoul-Henning Kamp 	delta = *tv;
9200af9731SPoul-Henning Kamp 	timevalsub(&delta, &tv1);
9394c8fcd8SPeter Wemm 
9494c8fcd8SPeter Wemm 	/*
959c8fff87SBruce Evans 	 * If the system is secure, we do not allow the time to be
96fcae3aa6SNick Sayer 	 * set to a value earlier than 1 second less than the highest
97fcae3aa6SNick Sayer 	 * time we have yet seen. The worst a miscreant can do in
98fcae3aa6SNick Sayer 	 * this circumstance is "freeze" time. He couldn't go
99fcae3aa6SNick Sayer 	 * back to the past.
100c0bd94a7SNick Sayer 	 *
101c0bd94a7SNick Sayer 	 * We similarly do not allow the clock to be stepped more
102c0bd94a7SNick Sayer 	 * than one second, nor more than once per second. This allows
103c0bd94a7SNick Sayer 	 * a miscreant to make the clock march double-time, but no worse.
10494c8fcd8SPeter Wemm 	 */
1057edfb592SJohn Baldwin 	if (securelevel_gt(td->td_ucred, 1) != 0) {
106fcae3aa6SNick Sayer 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
1073f92429aSMatt Jacob 			/*
108c0bd94a7SNick Sayer 			 * Update maxtime to latest time we've seen.
1093f92429aSMatt Jacob 			 */
110fcae3aa6SNick Sayer 			if (tv1.tv_sec > maxtime.tv_sec)
111fcae3aa6SNick Sayer 				maxtime = tv1;
112fcae3aa6SNick Sayer 			tv2 = *tv;
113fcae3aa6SNick Sayer 			timevalsub(&tv2, &maxtime);
114fcae3aa6SNick Sayer 			if (tv2.tv_sec < -1) {
1153f92429aSMatt Jacob 				tv->tv_sec = maxtime.tv_sec - 1;
116fcae3aa6SNick Sayer 				printf("Time adjustment clamped to -1 second\n");
117fcae3aa6SNick Sayer 			}
1183f92429aSMatt Jacob 		} else {
119c0bd94a7SNick Sayer 			if (tv1.tv_sec == laststep.tv_sec) {
120c0bd94a7SNick Sayer 				splx(s);
121c0bd94a7SNick Sayer 				return (EPERM);
122c0bd94a7SNick Sayer 			}
123c0bd94a7SNick Sayer 			if (delta.tv_sec > 1) {
124c0bd94a7SNick Sayer 				tv->tv_sec = tv1.tv_sec + 1;
125c0bd94a7SNick Sayer 				printf("Time adjustment clamped to +1 second\n");
126c0bd94a7SNick Sayer 			}
127c0bd94a7SNick Sayer 			laststep = *tv;
128fcae3aa6SNick Sayer 		}
1299c8fff87SBruce Evans 	}
1309c8fff87SBruce Evans 
1317ec73f64SPoul-Henning Kamp 	ts.tv_sec = tv->tv_sec;
1327ec73f64SPoul-Henning Kamp 	ts.tv_nsec = tv->tv_usec * 1000;
1337edfb592SJohn Baldwin 	mtx_lock(&Giant);
13491266b96SPoul-Henning Kamp 	tc_setclock(&ts);
13594c8fcd8SPeter Wemm 	(void) splsoftclock();
13694c8fcd8SPeter Wemm 	lease_updatetime(delta.tv_sec);
13794c8fcd8SPeter Wemm 	splx(s);
13894c8fcd8SPeter Wemm 	resettodr();
1397edfb592SJohn Baldwin 	mtx_unlock(&Giant);
14094c8fcd8SPeter Wemm 	return (0);
14194c8fcd8SPeter Wemm }
14294c8fcd8SPeter Wemm 
14394c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
14494c8fcd8SPeter Wemm struct clock_gettime_args {
14594c8fcd8SPeter Wemm 	clockid_t clock_id;
14694c8fcd8SPeter Wemm 	struct	timespec *tp;
14794c8fcd8SPeter Wemm };
14894c8fcd8SPeter Wemm #endif
149708e7684SPeter Wemm 
150fb99ab88SMatthew Dillon /*
151fb99ab88SMatthew Dillon  * MPSAFE
152fb99ab88SMatthew Dillon  */
15394c8fcd8SPeter Wemm /* ARGSUSED */
15494c8fcd8SPeter Wemm int
15591afe087SPoul-Henning Kamp clock_gettime(struct thread *td, struct clock_gettime_args *uap)
15694c8fcd8SPeter Wemm {
15794c8fcd8SPeter Wemm 	struct timespec ats;
15894c8fcd8SPeter Wemm 
159708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
16094c8fcd8SPeter Wemm 		return (EINVAL);
161fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
1627ec73f64SPoul-Henning Kamp 	nanotime(&ats);
163fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
164708e7684SPeter Wemm 	return (copyout(&ats, SCARG(uap, tp), sizeof(ats)));
16594c8fcd8SPeter Wemm }
16694c8fcd8SPeter Wemm 
16794c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
16894c8fcd8SPeter Wemm struct clock_settime_args {
16994c8fcd8SPeter Wemm 	clockid_t clock_id;
17094c8fcd8SPeter Wemm 	const struct	timespec *tp;
17194c8fcd8SPeter Wemm };
17294c8fcd8SPeter Wemm #endif
173708e7684SPeter Wemm 
174fb99ab88SMatthew Dillon /*
175fb99ab88SMatthew Dillon  * MPSAFE
176fb99ab88SMatthew Dillon  */
17794c8fcd8SPeter Wemm /* ARGSUSED */
17894c8fcd8SPeter Wemm int
17991afe087SPoul-Henning Kamp clock_settime(struct thread *td, struct clock_settime_args *uap)
18094c8fcd8SPeter Wemm {
18194c8fcd8SPeter Wemm 	struct timeval atv;
18294c8fcd8SPeter Wemm 	struct timespec ats;
18394c8fcd8SPeter Wemm 	int error;
18494c8fcd8SPeter Wemm 
18544731cabSJohn Baldwin 	if ((error = suser(td)) != 0)
1867edfb592SJohn Baldwin 		return (error);
1877edfb592SJohn Baldwin 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
1887edfb592SJohn Baldwin 		return (EINVAL);
18994c8fcd8SPeter Wemm 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
1907edfb592SJohn Baldwin 		return (error);
1917edfb592SJohn Baldwin 	if (ats.tv_nsec < 0 || ats.tv_nsec >= 1000000000)
1927edfb592SJohn Baldwin 		return (EINVAL);
193a0502b19SPoul-Henning Kamp 	/* XXX Don't convert nsec->usec and back */
19494c8fcd8SPeter Wemm 	TIMESPEC_TO_TIMEVAL(&atv, &ats);
1957edfb592SJohn Baldwin 	error = settime(td, &atv);
19694c8fcd8SPeter Wemm 	return (error);
19794c8fcd8SPeter Wemm }
19894c8fcd8SPeter Wemm 
19994c8fcd8SPeter Wemm #ifndef _SYS_SYSPROTO_H_
20094c8fcd8SPeter Wemm struct clock_getres_args {
20194c8fcd8SPeter Wemm 	clockid_t clock_id;
20294c8fcd8SPeter Wemm 	struct	timespec *tp;
20394c8fcd8SPeter Wemm };
20494c8fcd8SPeter Wemm #endif
205708e7684SPeter Wemm 
20694c8fcd8SPeter Wemm int
20791afe087SPoul-Henning Kamp clock_getres(struct thread *td, struct clock_getres_args *uap)
20894c8fcd8SPeter Wemm {
20994c8fcd8SPeter Wemm 	struct timespec ts;
210708e7684SPeter Wemm 	int error;
21194c8fcd8SPeter Wemm 
212708e7684SPeter Wemm 	if (SCARG(uap, clock_id) != CLOCK_REALTIME)
21394c8fcd8SPeter Wemm 		return (EINVAL);
214708e7684SPeter Wemm 	error = 0;
21594c8fcd8SPeter Wemm 	if (SCARG(uap, tp)) {
21694c8fcd8SPeter Wemm 		ts.tv_sec = 0;
217ac0653dcSBruce Evans 		/*
218ac0653dcSBruce Evans 		 * Round up the result of the division cheaply by adding 1.
219ac0653dcSBruce Evans 		 * Rounding up is especially important if rounding down
220ac0653dcSBruce Evans 		 * would give 0.  Perfect rounding is unimportant.
221ac0653dcSBruce Evans 		 */
222ac0653dcSBruce Evans 		ts.tv_nsec = 1000000000 / tc_getfrequency() + 1;
22394c8fcd8SPeter Wemm 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
22494c8fcd8SPeter Wemm 	}
225708e7684SPeter Wemm 	return (error);
22694c8fcd8SPeter Wemm }
22794c8fcd8SPeter Wemm 
22894c8fcd8SPeter Wemm static int nanowait;
22994c8fcd8SPeter Wemm 
2305b870b7bSPeter Wemm static int
23191afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2325b870b7bSPeter Wemm {
2335704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
23433841826SPoul-Henning Kamp 	struct timeval tv;
23533841826SPoul-Henning Kamp 	int error;
2365b870b7bSPeter Wemm 
2377d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
238708e7684SPeter Wemm 		return (EINVAL);
239d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2407d7fb492SBruce Evans 		return (0);
241c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
24200af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
24333841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
24433841826SPoul-Henning Kamp 	for (;;) {
24533841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
24633841826SPoul-Henning Kamp 		    tvtohz(&tv));
247c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
24833841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
24933841826SPoul-Henning Kamp 			if (error == ERESTART)
25094c8fcd8SPeter Wemm 				error = EINTR;
25133841826SPoul-Henning Kamp 			if (rmt != NULL) {
25233841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
25333841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
25433841826SPoul-Henning Kamp 					timespecclear(&ts);
25500af9731SPoul-Henning Kamp 				*rmt = ts;
25600af9731SPoul-Henning Kamp 			}
25733841826SPoul-Henning Kamp 			return (error);
25833841826SPoul-Henning Kamp 		}
25933841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
26033841826SPoul-Henning Kamp 			return (0);
2615704ba6aSPoul-Henning Kamp 		ts3 = ts;
2625704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2635704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
26433841826SPoul-Henning Kamp 	}
2655b870b7bSPeter Wemm }
26694c8fcd8SPeter Wemm 
2675b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2685b870b7bSPeter Wemm struct nanosleep_args {
2695b870b7bSPeter Wemm 	struct	timespec *rqtp;
2705b870b7bSPeter Wemm 	struct	timespec *rmtp;
2715b870b7bSPeter Wemm };
2725b870b7bSPeter Wemm #endif
2735b870b7bSPeter Wemm 
274fb99ab88SMatthew Dillon /*
275fb99ab88SMatthew Dillon  * MPSAFE
276fb99ab88SMatthew Dillon  */
2775b870b7bSPeter Wemm /* ARGSUSED */
2785b870b7bSPeter Wemm int
27991afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
2805b870b7bSPeter Wemm {
2815b870b7bSPeter Wemm 	struct timespec rmt, rqt;
282fb99ab88SMatthew Dillon 	int error;
2835b870b7bSPeter Wemm 
2845b870b7bSPeter Wemm 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
2855b870b7bSPeter Wemm 	if (error)
2865b870b7bSPeter Wemm 		return (error);
287fb99ab88SMatthew Dillon 
288fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
289fb99ab88SMatthew Dillon 	if (SCARG(uap, rmtp)) {
29002c58685SPoul-Henning Kamp 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
291fb99ab88SMatthew Dillon 		    VM_PROT_WRITE)) {
292fb99ab88SMatthew Dillon 			error = EFAULT;
293fb99ab88SMatthew Dillon 			goto done2;
294fb99ab88SMatthew Dillon 		}
295fb99ab88SMatthew Dillon 	}
296b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
297d59fbbf6SPeter Wemm 	if (error && SCARG(uap, rmtp)) {
298fb99ab88SMatthew Dillon 		int error2;
299fb99ab88SMatthew Dillon 
300708e7684SPeter Wemm 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
3015b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
302fb99ab88SMatthew Dillon 			error = error2;
303708e7684SPeter Wemm 	}
304fb99ab88SMatthew Dillon done2:
305fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
306708e7684SPeter Wemm 	return (error);
30794c8fcd8SPeter Wemm }
30894c8fcd8SPeter Wemm 
3095b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
310df8bae1dSRodney W. Grimes struct gettimeofday_args {
311df8bae1dSRodney W. Grimes 	struct	timeval *tp;
312df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
313df8bae1dSRodney W. Grimes };
314d2d3e875SBruce Evans #endif
315fb99ab88SMatthew Dillon /*
316fb99ab88SMatthew Dillon  * MPSAFE
317fb99ab88SMatthew Dillon  */
318df8bae1dSRodney W. Grimes /* ARGSUSED */
31926f9a767SRodney W. Grimes int
32091afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
321df8bae1dSRodney W. Grimes {
322df8bae1dSRodney W. Grimes 	struct timeval atv;
323df8bae1dSRodney W. Grimes 	int error = 0;
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes 	if (uap->tp) {
326df8bae1dSRodney W. Grimes 		microtime(&atv);
32701609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
328df8bae1dSRodney W. Grimes 	}
32921dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
33021dcdb38SPoul-Henning Kamp 		mtx_lock(&Giant);
33101609114SAlfred Perlstein 		error = copyout(&tz, uap->tzp, sizeof (tz));
332fb99ab88SMatthew Dillon 		mtx_unlock(&Giant);
33321dcdb38SPoul-Henning Kamp 	}
334df8bae1dSRodney W. Grimes 	return (error);
335df8bae1dSRodney W. Grimes }
336df8bae1dSRodney W. Grimes 
337d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
338df8bae1dSRodney W. Grimes struct settimeofday_args {
339df8bae1dSRodney W. Grimes 	struct	timeval *tv;
340df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
341df8bae1dSRodney W. Grimes };
342d2d3e875SBruce Evans #endif
343fb99ab88SMatthew Dillon /*
344fb99ab88SMatthew Dillon  * MPSAFE
345fb99ab88SMatthew Dillon  */
346df8bae1dSRodney W. Grimes /* ARGSUSED */
34726f9a767SRodney W. Grimes int
34891afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
349df8bae1dSRodney W. Grimes {
350708e7684SPeter Wemm 	struct timeval atv;
351df8bae1dSRodney W. Grimes 	struct timezone atz;
352fb99ab88SMatthew Dillon 	int error = 0;
353fb99ab88SMatthew Dillon 
35444731cabSJohn Baldwin 	if ((error = suser(td)))
3557edfb592SJohn Baldwin 		return (error);
356df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
357708e7684SPeter Wemm 	if (uap->tv) {
35801609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
3597edfb592SJohn Baldwin 			return (error);
3607edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3617edfb592SJohn Baldwin 			return (EINVAL);
362708e7684SPeter Wemm 	}
363df8bae1dSRodney W. Grimes 	if (uap->tzp &&
36401609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
3657edfb592SJohn Baldwin 		return (error);
3667edfb592SJohn Baldwin 
3677edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
3687edfb592SJohn Baldwin 		return (error);
3697edfb592SJohn Baldwin 	if (uap->tzp) {
3707edfb592SJohn Baldwin 		mtx_lock(&Giant);
371df8bae1dSRodney W. Grimes 		tz = atz;
372fb99ab88SMatthew Dillon 		mtx_unlock(&Giant);
3737edfb592SJohn Baldwin 	}
374fb99ab88SMatthew Dillon 	return (error);
375df8bae1dSRodney W. Grimes }
376df8bae1dSRodney W. Grimes /*
377df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
378df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
379df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
380df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
381df8bae1dSRodney W. Grimes  *
382df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
383df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
384df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
385df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
386df8bae1dSRodney W. Grimes  *
387df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
388df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
389df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
390df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
391df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
392df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
393df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
394df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
395df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
396df8bae1dSRodney W. Grimes  */
397d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
398df8bae1dSRodney W. Grimes struct getitimer_args {
399df8bae1dSRodney W. Grimes 	u_int	which;
400df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
401df8bae1dSRodney W. Grimes };
402d2d3e875SBruce Evans #endif
403fb99ab88SMatthew Dillon /*
404fb99ab88SMatthew Dillon  * MPSAFE
405fb99ab88SMatthew Dillon  */
406df8bae1dSRodney W. Grimes /* ARGSUSED */
40726f9a767SRodney W. Grimes int
40891afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
409df8bae1dSRodney W. Grimes {
410b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
411227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
412df8bae1dSRodney W. Grimes 	struct itimerval aitv;
413df8bae1dSRodney W. Grimes 	int s;
414fb99ab88SMatthew Dillon 	int error;
415df8bae1dSRodney W. Grimes 
416df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
417df8bae1dSRodney W. Grimes 		return (EINVAL);
418fb99ab88SMatthew Dillon 
419fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
420fb99ab88SMatthew Dillon 
421227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX still needed ? */
422df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
423df8bae1dSRodney W. Grimes 		/*
424ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
425df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
426df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
427df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
428df8bae1dSRodney W. Grimes 		 */
429df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
4304cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
431c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4324cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4334cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
434df8bae1dSRodney W. Grimes 			else
435227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
436227ee8a1SPoul-Henning Kamp 		}
437fb99ab88SMatthew Dillon 	} else {
438df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
439fb99ab88SMatthew Dillon 	}
440df8bae1dSRodney W. Grimes 	splx(s);
44101609114SAlfred Perlstein 	error = copyout(&aitv, uap->itv, sizeof (struct itimerval));
442fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
443fb99ab88SMatthew Dillon 	return(error);
444df8bae1dSRodney W. Grimes }
445df8bae1dSRodney W. Grimes 
446d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
447df8bae1dSRodney W. Grimes struct setitimer_args {
448df8bae1dSRodney W. Grimes 	u_int	which;
449df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
450df8bae1dSRodney W. Grimes };
451d2d3e875SBruce Evans #endif
452fb99ab88SMatthew Dillon /*
453fb99ab88SMatthew Dillon  * MPSAFE
454fb99ab88SMatthew Dillon  */
455df8bae1dSRodney W. Grimes /* ARGSUSED */
45626f9a767SRodney W. Grimes int
45791afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
458df8bae1dSRodney W. Grimes {
459b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
460df8bae1dSRodney W. Grimes 	struct itimerval aitv;
461227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
46291afe087SPoul-Henning Kamp 	struct itimerval *itvp;
463fb99ab88SMatthew Dillon 	int s, error = 0;
464df8bae1dSRodney W. Grimes 
465df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
466df8bae1dSRodney W. Grimes 		return (EINVAL);
467df8bae1dSRodney W. Grimes 	itvp = uap->itv;
46801609114SAlfred Perlstein 	if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
469df8bae1dSRodney W. Grimes 		return (error);
470fb99ab88SMatthew Dillon 
471fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
472fb99ab88SMatthew Dillon 
4730808c591SBruce Evans 	if ((uap->itv = uap->oitv) &&
474b40ce416SJulian Elischer 	    (error = getitimer(td, (struct getitimer_args *)uap))) {
475fb99ab88SMatthew Dillon 		goto done2;
476fb99ab88SMatthew Dillon 	}
477fb99ab88SMatthew Dillon 	if (itvp == 0) {
478fb99ab88SMatthew Dillon 		error = 0;
479fb99ab88SMatthew Dillon 		goto done2;
480fb99ab88SMatthew Dillon 	}
481fb99ab88SMatthew Dillon 	if (itimerfix(&aitv.it_value)) {
482fb99ab88SMatthew Dillon 		error = EINVAL;
483fb99ab88SMatthew Dillon 		goto done2;
484fb99ab88SMatthew Dillon 	}
485fb99ab88SMatthew Dillon 	if (!timevalisset(&aitv.it_value)) {
4864cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
487fb99ab88SMatthew Dillon 	} else if (itimerfix(&aitv.it_interval)) {
488fb99ab88SMatthew Dillon 		error = EINVAL;
489fb99ab88SMatthew Dillon 		goto done2;
490fb99ab88SMatthew Dillon 	}
491227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX: still needed ? */
492df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
4934cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
4944f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
4954cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value))
4964f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
4974f559836SJake Burkholder 			    realitexpire, p);
498c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
499bfe6c9faSPoul-Henning Kamp 		timevaladd(&aitv.it_value, &ctv);
500df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
501fb99ab88SMatthew Dillon 	} else {
502df8bae1dSRodney W. Grimes 		p->p_stats->p_timer[uap->which] = aitv;
503fb99ab88SMatthew Dillon 	}
504df8bae1dSRodney W. Grimes 	splx(s);
505fb99ab88SMatthew Dillon done2:
506fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
507fb99ab88SMatthew Dillon 	return (error);
508df8bae1dSRodney W. Grimes }
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes /*
511df8bae1dSRodney W. Grimes  * Real interval timer expired:
512df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
513df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
514df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
515df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
516df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
517c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5189207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5199207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5209207f00aSBruce Evans  * interrupt even when we're delayed.
521df8bae1dSRodney W. Grimes  */
522df8bae1dSRodney W. Grimes void
52391afe087SPoul-Henning Kamp realitexpire(void *arg)
524df8bae1dSRodney W. Grimes {
52591afe087SPoul-Henning Kamp 	struct proc *p;
526bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
527df8bae1dSRodney W. Grimes 	int s;
528df8bae1dSRodney W. Grimes 
529df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
53037824023SJohn Baldwin 	PROC_LOCK(p);
531df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5324cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5334cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
53437824023SJohn Baldwin 		PROC_UNLOCK(p);
535df8bae1dSRodney W. Grimes 		return;
536df8bae1dSRodney W. Grimes 	}
537df8bae1dSRodney W. Grimes 	for (;;) {
538227ee8a1SPoul-Henning Kamp 		s = splclock(); /* XXX: still neeeded ? */
539df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
540df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
541c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5424cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
543bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
544bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5454f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5464f559836SJake Burkholder 			    realitexpire, p);
547df8bae1dSRodney W. Grimes 			splx(s);
54837824023SJohn Baldwin 			PROC_UNLOCK(p);
549df8bae1dSRodney W. Grimes 			return;
550df8bae1dSRodney W. Grimes 		}
551df8bae1dSRodney W. Grimes 		splx(s);
552df8bae1dSRodney W. Grimes 	}
55337824023SJohn Baldwin 	/*NOTREACHED*/
554df8bae1dSRodney W. Grimes }
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes /*
557df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
558df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
559df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
560df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
561df8bae1dSRodney W. Grimes  */
56226f9a767SRodney W. Grimes int
56391afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
564df8bae1dSRodney W. Grimes {
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
567df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
568df8bae1dSRodney W. Grimes 		return (EINVAL);
569df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
570df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
571df8bae1dSRodney W. Grimes 	return (0);
572df8bae1dSRodney W. Grimes }
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes /*
575df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
576df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
577df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
578df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
579df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
580df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
581df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
582df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
583df8bae1dSRodney W. Grimes  */
58426f9a767SRodney W. Grimes int
58591afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
586df8bae1dSRodney W. Grimes {
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
589df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
590df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
591df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
592df8bae1dSRodney W. Grimes 			goto expire;
593df8bae1dSRodney W. Grimes 		}
594df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
595df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
596df8bae1dSRodney W. Grimes 	}
597df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
598df8bae1dSRodney W. Grimes 	usec = 0;
5994cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
600df8bae1dSRodney W. Grimes 		return (1);
601df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
602df8bae1dSRodney W. Grimes expire:
6034cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
604df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
605df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
606df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
607df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
608df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
609df8bae1dSRodney W. Grimes 		}
610df8bae1dSRodney W. Grimes 	} else
611df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
612df8bae1dSRodney W. Grimes 	return (0);
613df8bae1dSRodney W. Grimes }
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes /*
616df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
617df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
618df8bae1dSRodney W. Grimes  * results which are before the beginning,
619df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
620df8bae1dSRodney W. Grimes  * Caveat emptor.
621df8bae1dSRodney W. Grimes  */
62226f9a767SRodney W. Grimes void
62391afe087SPoul-Henning Kamp timevaladd(struct timeval *t1, struct timeval *t2)
624df8bae1dSRodney W. Grimes {
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
627df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
628df8bae1dSRodney W. Grimes 	timevalfix(t1);
629df8bae1dSRodney W. Grimes }
630df8bae1dSRodney W. Grimes 
63126f9a767SRodney W. Grimes void
63291afe087SPoul-Henning Kamp timevalsub(struct timeval *t1, struct timeval *t2)
633df8bae1dSRodney W. Grimes {
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
636df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
637df8bae1dSRodney W. Grimes 	timevalfix(t1);
638df8bae1dSRodney W. Grimes }
639df8bae1dSRodney W. Grimes 
64087b6de2bSPoul-Henning Kamp static void
64191afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
642df8bae1dSRodney W. Grimes {
643df8bae1dSRodney W. Grimes 
644df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
645df8bae1dSRodney W. Grimes 		t1->tv_sec--;
646df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
647df8bae1dSRodney W. Grimes 	}
648df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
649df8bae1dSRodney W. Grimes 		t1->tv_sec++;
650df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
651df8bae1dSRodney W. Grimes 	}
652df8bae1dSRodney W. Grimes }
653