xref: /freebsd/sys/kern/kern_time.c (revision 91afe0874da1a46b1bc52ee93e9959575bd067db)
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;
217b4a1d0deSPoul-Henning Kamp 		ts.tv_nsec = 1000000000 / tc_getfrequency();
21894c8fcd8SPeter Wemm 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
21994c8fcd8SPeter Wemm 	}
220708e7684SPeter Wemm 	return (error);
22194c8fcd8SPeter Wemm }
22294c8fcd8SPeter Wemm 
22394c8fcd8SPeter Wemm static int nanowait;
22494c8fcd8SPeter Wemm 
2255b870b7bSPeter Wemm static int
22691afe087SPoul-Henning Kamp nanosleep1(struct thread *td, struct timespec *rqt, struct timespec *rmt)
2275b870b7bSPeter Wemm {
2285704ba6aSPoul-Henning Kamp 	struct timespec ts, ts2, ts3;
22933841826SPoul-Henning Kamp 	struct timeval tv;
23033841826SPoul-Henning Kamp 	int error;
2315b870b7bSPeter Wemm 
2327d7fb492SBruce Evans 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
233708e7684SPeter Wemm 		return (EINVAL);
234d254af07SMatthew Dillon 	if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
2357d7fb492SBruce Evans 		return (0);
236c21410e1SPoul-Henning Kamp 	getnanouptime(&ts);
23700af9731SPoul-Henning Kamp 	timespecadd(&ts, rqt);
23833841826SPoul-Henning Kamp 	TIMESPEC_TO_TIMEVAL(&tv, rqt);
23933841826SPoul-Henning Kamp 	for (;;) {
24033841826SPoul-Henning Kamp 		error = tsleep(&nanowait, PWAIT | PCATCH, "nanslp",
24133841826SPoul-Henning Kamp 		    tvtohz(&tv));
242c21410e1SPoul-Henning Kamp 		getnanouptime(&ts2);
24333841826SPoul-Henning Kamp 		if (error != EWOULDBLOCK) {
24433841826SPoul-Henning Kamp 			if (error == ERESTART)
24594c8fcd8SPeter Wemm 				error = EINTR;
24633841826SPoul-Henning Kamp 			if (rmt != NULL) {
24733841826SPoul-Henning Kamp 				timespecsub(&ts, &ts2);
24833841826SPoul-Henning Kamp 				if (ts.tv_sec < 0)
24933841826SPoul-Henning Kamp 					timespecclear(&ts);
25000af9731SPoul-Henning Kamp 				*rmt = ts;
25100af9731SPoul-Henning Kamp 			}
25233841826SPoul-Henning Kamp 			return (error);
25333841826SPoul-Henning Kamp 		}
25433841826SPoul-Henning Kamp 		if (timespeccmp(&ts2, &ts, >=))
25533841826SPoul-Henning Kamp 			return (0);
2565704ba6aSPoul-Henning Kamp 		ts3 = ts;
2575704ba6aSPoul-Henning Kamp 		timespecsub(&ts3, &ts2);
2585704ba6aSPoul-Henning Kamp 		TIMESPEC_TO_TIMEVAL(&tv, &ts3);
25933841826SPoul-Henning Kamp 	}
2605b870b7bSPeter Wemm }
26194c8fcd8SPeter Wemm 
2625b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
2635b870b7bSPeter Wemm struct nanosleep_args {
2645b870b7bSPeter Wemm 	struct	timespec *rqtp;
2655b870b7bSPeter Wemm 	struct	timespec *rmtp;
2665b870b7bSPeter Wemm };
2675b870b7bSPeter Wemm #endif
2685b870b7bSPeter Wemm 
269fb99ab88SMatthew Dillon /*
270fb99ab88SMatthew Dillon  * MPSAFE
271fb99ab88SMatthew Dillon  */
2725b870b7bSPeter Wemm /* ARGSUSED */
2735b870b7bSPeter Wemm int
27491afe087SPoul-Henning Kamp nanosleep(struct thread *td, struct nanosleep_args *uap)
2755b870b7bSPeter Wemm {
2765b870b7bSPeter Wemm 	struct timespec rmt, rqt;
277fb99ab88SMatthew Dillon 	int error;
2785b870b7bSPeter Wemm 
2795b870b7bSPeter Wemm 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(rqt));
2805b870b7bSPeter Wemm 	if (error)
2815b870b7bSPeter Wemm 		return (error);
282fb99ab88SMatthew Dillon 
283fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
284fb99ab88SMatthew Dillon 	if (SCARG(uap, rmtp)) {
28502c58685SPoul-Henning Kamp 		if (!useracc((caddr_t)SCARG(uap, rmtp), sizeof(rmt),
286fb99ab88SMatthew Dillon 		    VM_PROT_WRITE)) {
287fb99ab88SMatthew Dillon 			error = EFAULT;
288fb99ab88SMatthew Dillon 			goto done2;
289fb99ab88SMatthew Dillon 		}
290fb99ab88SMatthew Dillon 	}
291b40ce416SJulian Elischer 	error = nanosleep1(td, &rqt, &rmt);
292d59fbbf6SPeter Wemm 	if (error && SCARG(uap, rmtp)) {
293fb99ab88SMatthew Dillon 		int error2;
294fb99ab88SMatthew Dillon 
295708e7684SPeter Wemm 		error2 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
2965b870b7bSPeter Wemm 		if (error2)	/* XXX shouldn't happen, did useracc() above */
297fb99ab88SMatthew Dillon 			error = error2;
298708e7684SPeter Wemm 	}
299fb99ab88SMatthew Dillon done2:
300fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
301708e7684SPeter Wemm 	return (error);
30294c8fcd8SPeter Wemm }
30394c8fcd8SPeter Wemm 
3045b870b7bSPeter Wemm #ifndef _SYS_SYSPROTO_H_
305df8bae1dSRodney W. Grimes struct gettimeofday_args {
306df8bae1dSRodney W. Grimes 	struct	timeval *tp;
307df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
308df8bae1dSRodney W. Grimes };
309d2d3e875SBruce Evans #endif
310fb99ab88SMatthew Dillon /*
311fb99ab88SMatthew Dillon  * MPSAFE
312fb99ab88SMatthew Dillon  */
313df8bae1dSRodney W. Grimes /* ARGSUSED */
31426f9a767SRodney W. Grimes int
31591afe087SPoul-Henning Kamp gettimeofday(struct thread *td, struct gettimeofday_args *uap)
316df8bae1dSRodney W. Grimes {
317df8bae1dSRodney W. Grimes 	struct timeval atv;
318df8bae1dSRodney W. Grimes 	int error = 0;
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes 	if (uap->tp) {
321df8bae1dSRodney W. Grimes 		microtime(&atv);
32201609114SAlfred Perlstein 		error = copyout(&atv, uap->tp, sizeof (atv));
323df8bae1dSRodney W. Grimes 	}
32421dcdb38SPoul-Henning Kamp 	if (error == 0 && uap->tzp != NULL) {
32521dcdb38SPoul-Henning Kamp 		mtx_lock(&Giant);
32601609114SAlfred Perlstein 		error = copyout(&tz, uap->tzp, sizeof (tz));
327fb99ab88SMatthew Dillon 		mtx_unlock(&Giant);
32821dcdb38SPoul-Henning Kamp 	}
329df8bae1dSRodney W. Grimes 	return (error);
330df8bae1dSRodney W. Grimes }
331df8bae1dSRodney W. Grimes 
332d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
333df8bae1dSRodney W. Grimes struct settimeofday_args {
334df8bae1dSRodney W. Grimes 	struct	timeval *tv;
335df8bae1dSRodney W. Grimes 	struct	timezone *tzp;
336df8bae1dSRodney W. Grimes };
337d2d3e875SBruce Evans #endif
338fb99ab88SMatthew Dillon /*
339fb99ab88SMatthew Dillon  * MPSAFE
340fb99ab88SMatthew Dillon  */
341df8bae1dSRodney W. Grimes /* ARGSUSED */
34226f9a767SRodney W. Grimes int
34391afe087SPoul-Henning Kamp settimeofday(struct thread *td, struct settimeofday_args *uap)
344df8bae1dSRodney W. Grimes {
345708e7684SPeter Wemm 	struct timeval atv;
346df8bae1dSRodney W. Grimes 	struct timezone atz;
347fb99ab88SMatthew Dillon 	int error = 0;
348fb99ab88SMatthew Dillon 
34944731cabSJohn Baldwin 	if ((error = suser(td)))
3507edfb592SJohn Baldwin 		return (error);
351df8bae1dSRodney W. Grimes 	/* Verify all parameters before changing time. */
352708e7684SPeter Wemm 	if (uap->tv) {
35301609114SAlfred Perlstein 		if ((error = copyin(uap->tv, &atv, sizeof(atv))))
3547edfb592SJohn Baldwin 			return (error);
3557edfb592SJohn Baldwin 		if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
3567edfb592SJohn Baldwin 			return (EINVAL);
357708e7684SPeter Wemm 	}
358df8bae1dSRodney W. Grimes 	if (uap->tzp &&
35901609114SAlfred Perlstein 	    (error = copyin(uap->tzp, &atz, sizeof(atz))))
3607edfb592SJohn Baldwin 		return (error);
3617edfb592SJohn Baldwin 
3627edfb592SJohn Baldwin 	if (uap->tv && (error = settime(td, &atv)))
3637edfb592SJohn Baldwin 		return (error);
3647edfb592SJohn Baldwin 	if (uap->tzp) {
3657edfb592SJohn Baldwin 		mtx_lock(&Giant);
366df8bae1dSRodney W. Grimes 		tz = atz;
367fb99ab88SMatthew Dillon 		mtx_unlock(&Giant);
3687edfb592SJohn Baldwin 	}
369fb99ab88SMatthew Dillon 	return (error);
370df8bae1dSRodney W. Grimes }
371df8bae1dSRodney W. Grimes /*
372df8bae1dSRodney W. Grimes  * Get value of an interval timer.  The process virtual and
373df8bae1dSRodney W. Grimes  * profiling virtual time timers are kept in the p_stats area, since
374df8bae1dSRodney W. Grimes  * they can be swapped out.  These are kept internally in the
375df8bae1dSRodney W. Grimes  * way they are specified externally: in time until they expire.
376df8bae1dSRodney W. Grimes  *
377df8bae1dSRodney W. Grimes  * The real time interval timer is kept in the process table slot
378df8bae1dSRodney W. Grimes  * for the process, and its value (it_value) is kept as an
379df8bae1dSRodney W. Grimes  * absolute time rather than as a delta, so that it is easy to keep
380df8bae1dSRodney W. Grimes  * periodic real-time signals from drifting.
381df8bae1dSRodney W. Grimes  *
382df8bae1dSRodney W. Grimes  * Virtual time timers are processed in the hardclock() routine of
383df8bae1dSRodney W. Grimes  * kern_clock.c.  The real time timer is processed by a timeout
384df8bae1dSRodney W. Grimes  * routine, called from the softclock() routine.  Since a callout
385df8bae1dSRodney W. Grimes  * may be delayed in real time due to interrupt processing in the system,
386df8bae1dSRodney W. Grimes  * it is possible for the real time timeout routine (realitexpire, given below),
387df8bae1dSRodney W. Grimes  * to be delayed in real time past when it is supposed to occur.  It
388df8bae1dSRodney W. Grimes  * does not suffice, therefore, to reload the real timer .it_value from the
389df8bae1dSRodney W. Grimes  * real time timers .it_interval.  Rather, we compute the next time in
390df8bae1dSRodney W. Grimes  * absolute time the timer should go off.
391df8bae1dSRodney W. Grimes  */
392d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
393df8bae1dSRodney W. Grimes struct getitimer_args {
394df8bae1dSRodney W. Grimes 	u_int	which;
395df8bae1dSRodney W. Grimes 	struct	itimerval *itv;
396df8bae1dSRodney W. Grimes };
397d2d3e875SBruce Evans #endif
398fb99ab88SMatthew Dillon /*
399fb99ab88SMatthew Dillon  * MPSAFE
400fb99ab88SMatthew Dillon  */
401df8bae1dSRodney W. Grimes /* ARGSUSED */
40226f9a767SRodney W. Grimes int
40391afe087SPoul-Henning Kamp getitimer(struct thread *td, struct getitimer_args *uap)
404df8bae1dSRodney W. Grimes {
405b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
406227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
407df8bae1dSRodney W. Grimes 	struct itimerval aitv;
408df8bae1dSRodney W. Grimes 	int s;
409fb99ab88SMatthew Dillon 	int error;
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
412df8bae1dSRodney W. Grimes 		return (EINVAL);
413fb99ab88SMatthew Dillon 
414fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
415fb99ab88SMatthew Dillon 
416227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX still needed ? */
417df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
418df8bae1dSRodney W. Grimes 		/*
419ee002b68SBruce Evans 		 * Convert from absolute to relative time in .it_value
420df8bae1dSRodney W. Grimes 		 * part of real time timer.  If time for real time timer
421df8bae1dSRodney W. Grimes 		 * has passed return 0, else return difference between
422df8bae1dSRodney W. Grimes 		 * current time and time for the timer to go off.
423df8bae1dSRodney W. Grimes 		 */
424df8bae1dSRodney W. Grimes 		aitv = p->p_realtimer;
4254cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value)) {
426c21410e1SPoul-Henning Kamp 			getmicrouptime(&ctv);
4274cf41af3SPoul-Henning Kamp 			if (timevalcmp(&aitv.it_value, &ctv, <))
4284cf41af3SPoul-Henning Kamp 				timevalclear(&aitv.it_value);
429df8bae1dSRodney W. Grimes 			else
430227ee8a1SPoul-Henning Kamp 				timevalsub(&aitv.it_value, &ctv);
431227ee8a1SPoul-Henning Kamp 		}
432fb99ab88SMatthew Dillon 	} else {
433df8bae1dSRodney W. Grimes 		aitv = p->p_stats->p_timer[uap->which];
434fb99ab88SMatthew Dillon 	}
435df8bae1dSRodney W. Grimes 	splx(s);
43601609114SAlfred Perlstein 	error = copyout(&aitv, uap->itv, sizeof (struct itimerval));
437fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
438fb99ab88SMatthew Dillon 	return(error);
439df8bae1dSRodney W. Grimes }
440df8bae1dSRodney W. Grimes 
441d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
442df8bae1dSRodney W. Grimes struct setitimer_args {
443df8bae1dSRodney W. Grimes 	u_int	which;
444df8bae1dSRodney W. Grimes 	struct	itimerval *itv, *oitv;
445df8bae1dSRodney W. Grimes };
446d2d3e875SBruce Evans #endif
447fb99ab88SMatthew Dillon /*
448fb99ab88SMatthew Dillon  * MPSAFE
449fb99ab88SMatthew Dillon  */
450df8bae1dSRodney W. Grimes /* ARGSUSED */
45126f9a767SRodney W. Grimes int
45291afe087SPoul-Henning Kamp setitimer(struct thread *td, struct setitimer_args *uap)
453df8bae1dSRodney W. Grimes {
454b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
455df8bae1dSRodney W. Grimes 	struct itimerval aitv;
456227ee8a1SPoul-Henning Kamp 	struct timeval ctv;
45791afe087SPoul-Henning Kamp 	struct itimerval *itvp;
458fb99ab88SMatthew Dillon 	int s, error = 0;
459df8bae1dSRodney W. Grimes 
460df8bae1dSRodney W. Grimes 	if (uap->which > ITIMER_PROF)
461df8bae1dSRodney W. Grimes 		return (EINVAL);
462df8bae1dSRodney W. Grimes 	itvp = uap->itv;
46301609114SAlfred Perlstein 	if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
464df8bae1dSRodney W. Grimes 		return (error);
465fb99ab88SMatthew Dillon 
466fb99ab88SMatthew Dillon 	mtx_lock(&Giant);
467fb99ab88SMatthew Dillon 
4680808c591SBruce Evans 	if ((uap->itv = uap->oitv) &&
469b40ce416SJulian Elischer 	    (error = getitimer(td, (struct getitimer_args *)uap))) {
470fb99ab88SMatthew Dillon 		goto done2;
471fb99ab88SMatthew Dillon 	}
472fb99ab88SMatthew Dillon 	if (itvp == 0) {
473fb99ab88SMatthew Dillon 		error = 0;
474fb99ab88SMatthew Dillon 		goto done2;
475fb99ab88SMatthew Dillon 	}
476fb99ab88SMatthew Dillon 	if (itimerfix(&aitv.it_value)) {
477fb99ab88SMatthew Dillon 		error = EINVAL;
478fb99ab88SMatthew Dillon 		goto done2;
479fb99ab88SMatthew Dillon 	}
480fb99ab88SMatthew Dillon 	if (!timevalisset(&aitv.it_value)) {
4814cf41af3SPoul-Henning Kamp 		timevalclear(&aitv.it_interval);
482fb99ab88SMatthew Dillon 	} else if (itimerfix(&aitv.it_interval)) {
483fb99ab88SMatthew Dillon 		error = EINVAL;
484fb99ab88SMatthew Dillon 		goto done2;
485fb99ab88SMatthew Dillon 	}
486227ee8a1SPoul-Henning Kamp 	s = splclock(); /* XXX: still needed ? */
487df8bae1dSRodney W. Grimes 	if (uap->which == ITIMER_REAL) {
4884cf41af3SPoul-Henning Kamp 		if (timevalisset(&p->p_realtimer.it_value))
4894f559836SJake Burkholder 			callout_stop(&p->p_itcallout);
4904cf41af3SPoul-Henning Kamp 		if (timevalisset(&aitv.it_value))
4914f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&aitv.it_value),
4924f559836SJake Burkholder 			    realitexpire, p);
493c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
494bfe6c9faSPoul-Henning Kamp 		timevaladd(&aitv.it_value, &ctv);
495df8bae1dSRodney W. Grimes 		p->p_realtimer = aitv;
496fb99ab88SMatthew Dillon 	} else {
497df8bae1dSRodney W. Grimes 		p->p_stats->p_timer[uap->which] = aitv;
498fb99ab88SMatthew Dillon 	}
499df8bae1dSRodney W. Grimes 	splx(s);
500fb99ab88SMatthew Dillon done2:
501fb99ab88SMatthew Dillon 	mtx_unlock(&Giant);
502fb99ab88SMatthew Dillon 	return (error);
503df8bae1dSRodney W. Grimes }
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes /*
506df8bae1dSRodney W. Grimes  * Real interval timer expired:
507df8bae1dSRodney W. Grimes  * send process whose timer expired an alarm signal.
508df8bae1dSRodney W. Grimes  * If time is not set up to reload, then just return.
509df8bae1dSRodney W. Grimes  * Else compute next time timer should go off which is > current time.
510df8bae1dSRodney W. Grimes  * This is where delay in processing this timeout causes multiple
511df8bae1dSRodney W. Grimes  * SIGALRM calls to be compressed into one.
512c8b47828SBruce Evans  * tvtohz() always adds 1 to allow for the time until the next clock
5139207f00aSBruce Evans  * interrupt being strictly less than 1 clock tick, but we don't want
5149207f00aSBruce Evans  * that here since we want to appear to be in sync with the clock
5159207f00aSBruce Evans  * interrupt even when we're delayed.
516df8bae1dSRodney W. Grimes  */
517df8bae1dSRodney W. Grimes void
51891afe087SPoul-Henning Kamp realitexpire(void *arg)
519df8bae1dSRodney W. Grimes {
52091afe087SPoul-Henning Kamp 	struct proc *p;
521bfe6c9faSPoul-Henning Kamp 	struct timeval ctv, ntv;
522df8bae1dSRodney W. Grimes 	int s;
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes 	p = (struct proc *)arg;
52537824023SJohn Baldwin 	PROC_LOCK(p);
526df8bae1dSRodney W. Grimes 	psignal(p, SIGALRM);
5274cf41af3SPoul-Henning Kamp 	if (!timevalisset(&p->p_realtimer.it_interval)) {
5284cf41af3SPoul-Henning Kamp 		timevalclear(&p->p_realtimer.it_value);
52937824023SJohn Baldwin 		PROC_UNLOCK(p);
530df8bae1dSRodney W. Grimes 		return;
531df8bae1dSRodney W. Grimes 	}
532df8bae1dSRodney W. Grimes 	for (;;) {
533227ee8a1SPoul-Henning Kamp 		s = splclock(); /* XXX: still neeeded ? */
534df8bae1dSRodney W. Grimes 		timevaladd(&p->p_realtimer.it_value,
535df8bae1dSRodney W. Grimes 		    &p->p_realtimer.it_interval);
536c21410e1SPoul-Henning Kamp 		getmicrouptime(&ctv);
5374cf41af3SPoul-Henning Kamp 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
538bfe6c9faSPoul-Henning Kamp 			ntv = p->p_realtimer.it_value;
539bfe6c9faSPoul-Henning Kamp 			timevalsub(&ntv, &ctv);
5404f559836SJake Burkholder 			callout_reset(&p->p_itcallout, tvtohz(&ntv) - 1,
5414f559836SJake Burkholder 			    realitexpire, p);
542df8bae1dSRodney W. Grimes 			splx(s);
54337824023SJohn Baldwin 			PROC_UNLOCK(p);
544df8bae1dSRodney W. Grimes 			return;
545df8bae1dSRodney W. Grimes 		}
546df8bae1dSRodney W. Grimes 		splx(s);
547df8bae1dSRodney W. Grimes 	}
54837824023SJohn Baldwin 	/*NOTREACHED*/
549df8bae1dSRodney W. Grimes }
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes /*
552df8bae1dSRodney W. Grimes  * Check that a proposed value to load into the .it_value or
553df8bae1dSRodney W. Grimes  * .it_interval part of an interval timer is acceptable, and
554df8bae1dSRodney W. Grimes  * fix it to have at least minimal value (i.e. if it is less
555df8bae1dSRodney W. Grimes  * than the resolution of the clock, round it up.)
556df8bae1dSRodney W. Grimes  */
55726f9a767SRodney W. Grimes int
55891afe087SPoul-Henning Kamp itimerfix(struct timeval *tv)
559df8bae1dSRodney W. Grimes {
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
562df8bae1dSRodney W. Grimes 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
563df8bae1dSRodney W. Grimes 		return (EINVAL);
564df8bae1dSRodney W. Grimes 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
565df8bae1dSRodney W. Grimes 		tv->tv_usec = tick;
566df8bae1dSRodney W. Grimes 	return (0);
567df8bae1dSRodney W. Grimes }
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes /*
570df8bae1dSRodney W. Grimes  * Decrement an interval timer by a specified number
571df8bae1dSRodney W. Grimes  * of microseconds, which must be less than a second,
572df8bae1dSRodney W. Grimes  * i.e. < 1000000.  If the timer expires, then reload
573df8bae1dSRodney W. Grimes  * it.  In this case, carry over (usec - old value) to
574df8bae1dSRodney W. Grimes  * reduce the value reloaded into the timer so that
575df8bae1dSRodney W. Grimes  * the timer does not drift.  This routine assumes
576df8bae1dSRodney W. Grimes  * that it is called in a context where the timers
577df8bae1dSRodney W. Grimes  * on which it is operating cannot change in value.
578df8bae1dSRodney W. Grimes  */
57926f9a767SRodney W. Grimes int
58091afe087SPoul-Henning Kamp itimerdecr(struct itimerval *itp, int usec)
581df8bae1dSRodney W. Grimes {
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes 	if (itp->it_value.tv_usec < usec) {
584df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_sec == 0) {
585df8bae1dSRodney W. Grimes 			/* expired, and already in next interval */
586df8bae1dSRodney W. Grimes 			usec -= itp->it_value.tv_usec;
587df8bae1dSRodney W. Grimes 			goto expire;
588df8bae1dSRodney W. Grimes 		}
589df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec += 1000000;
590df8bae1dSRodney W. Grimes 		itp->it_value.tv_sec--;
591df8bae1dSRodney W. Grimes 	}
592df8bae1dSRodney W. Grimes 	itp->it_value.tv_usec -= usec;
593df8bae1dSRodney W. Grimes 	usec = 0;
5944cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_value))
595df8bae1dSRodney W. Grimes 		return (1);
596df8bae1dSRodney W. Grimes 	/* expired, exactly at end of interval */
597df8bae1dSRodney W. Grimes expire:
5984cf41af3SPoul-Henning Kamp 	if (timevalisset(&itp->it_interval)) {
599df8bae1dSRodney W. Grimes 		itp->it_value = itp->it_interval;
600df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec -= usec;
601df8bae1dSRodney W. Grimes 		if (itp->it_value.tv_usec < 0) {
602df8bae1dSRodney W. Grimes 			itp->it_value.tv_usec += 1000000;
603df8bae1dSRodney W. Grimes 			itp->it_value.tv_sec--;
604df8bae1dSRodney W. Grimes 		}
605df8bae1dSRodney W. Grimes 	} else
606df8bae1dSRodney W. Grimes 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
607df8bae1dSRodney W. Grimes 	return (0);
608df8bae1dSRodney W. Grimes }
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes /*
611df8bae1dSRodney W. Grimes  * Add and subtract routines for timevals.
612df8bae1dSRodney W. Grimes  * N.B.: subtract routine doesn't deal with
613df8bae1dSRodney W. Grimes  * results which are before the beginning,
614df8bae1dSRodney W. Grimes  * it just gets very confused in this case.
615df8bae1dSRodney W. Grimes  * Caveat emptor.
616df8bae1dSRodney W. Grimes  */
61726f9a767SRodney W. Grimes void
61891afe087SPoul-Henning Kamp timevaladd(struct timeval *t1, struct timeval *t2)
619df8bae1dSRodney W. Grimes {
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes 	t1->tv_sec += t2->tv_sec;
622df8bae1dSRodney W. Grimes 	t1->tv_usec += t2->tv_usec;
623df8bae1dSRodney W. Grimes 	timevalfix(t1);
624df8bae1dSRodney W. Grimes }
625df8bae1dSRodney W. Grimes 
62626f9a767SRodney W. Grimes void
62791afe087SPoul-Henning Kamp timevalsub(struct timeval *t1, struct timeval *t2)
628df8bae1dSRodney W. Grimes {
629df8bae1dSRodney W. Grimes 
630df8bae1dSRodney W. Grimes 	t1->tv_sec -= t2->tv_sec;
631df8bae1dSRodney W. Grimes 	t1->tv_usec -= t2->tv_usec;
632df8bae1dSRodney W. Grimes 	timevalfix(t1);
633df8bae1dSRodney W. Grimes }
634df8bae1dSRodney W. Grimes 
63587b6de2bSPoul-Henning Kamp static void
63691afe087SPoul-Henning Kamp timevalfix(struct timeval *t1)
637df8bae1dSRodney W. Grimes {
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 	if (t1->tv_usec < 0) {
640df8bae1dSRodney W. Grimes 		t1->tv_sec--;
641df8bae1dSRodney W. Grimes 		t1->tv_usec += 1000000;
642df8bae1dSRodney W. Grimes 	}
643df8bae1dSRodney W. Grimes 	if (t1->tv_usec >= 1000000) {
644df8bae1dSRodney W. Grimes 		t1->tv_sec++;
645df8bae1dSRodney W. Grimes 		t1->tv_usec -= 1000000;
646df8bae1dSRodney W. Grimes 	}
647df8bae1dSRodney W. Grimes }
648