xref: /original-bsd/sys/kern/kern_time.c (revision 2c51c3e4)
1dcae39bdSmckusick /*
2014695a9Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3014695a9Smckusick  * All rights reserved.
4dcae39bdSmckusick  *
5014695a9Smckusick  * Redistribution and use in source and binary forms are permitted
6014695a9Smckusick  * provided that the above copyright notice and this paragraph are
7014695a9Smckusick  * duplicated in all such forms and that any documentation,
8014695a9Smckusick  * advertising materials, and other materials related to such
9014695a9Smckusick  * distribution and use acknowledge that the software was developed
10014695a9Smckusick  * by the University of California, Berkeley.  The name of the
11014695a9Smckusick  * University may not be used to endorse or promote products derived
12014695a9Smckusick  * from this software without specific prior written permission.
13014695a9Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14014695a9Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15014695a9Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16014695a9Smckusick  *
17*2c51c3e4Skarels  *	@(#)kern_time.c	7.12 (Berkeley) 06/28/90
18dcae39bdSmckusick  */
19db51853bSsam 
2048846e23Sbloom #include "param.h"
21*2c51c3e4Skarels #include "user.h"
2248846e23Sbloom #include "kernel.h"
2348846e23Sbloom #include "proc.h"
24ef678427Sroot 
25ae1aeb7bSmckusick #include "machine/reg.h"
26ae1aeb7bSmckusick #include "machine/cpu.h"
2767a72975Skarels 
2872762883Sroot /*
2972762883Sroot  * Time of day and interval timer support.
3034eef8ebSroot  *
3134eef8ebSroot  * These routines provide the kernel entry points to get and set
3234eef8ebSroot  * the time-of-day and per-process interval timers.  Subroutines
3334eef8ebSroot  * here provide support for adding and subtracting timeval structures
3434eef8ebSroot  * and decrementing interval timers, optionally reloading the interval
3534eef8ebSroot  * timers when they expire.
3672762883Sroot  */
3772762883Sroot 
38d1ec48c2Skarels /* ARGSUSED */
39d1ec48c2Skarels gettimeofday(p, uap, retval)
40d1ec48c2Skarels 	struct proc *p;
41d1ec48c2Skarels 	register struct args {
42ef678427Sroot 		struct	timeval *tp;
43ef678427Sroot 		struct	timezone *tzp;
44d1ec48c2Skarels 	} *uap;
45d1ec48c2Skarels 	int *retval;
46d1ec48c2Skarels {
47ef678427Sroot 	struct timeval atv;
48d1ec48c2Skarels 	int error = 0;
49ef678427Sroot 
502da4521eSbostic 	if (uap->tp) {
5170dbe469Skarels 		microtime(&atv);
52d1ec48c2Skarels 		if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
53d1ec48c2Skarels 		    sizeof (atv)))
54*2c51c3e4Skarels 			return (error);
552da4521eSbostic 	}
562da4521eSbostic 	if (uap->tzp)
57d1ec48c2Skarels 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
582da4521eSbostic 		    sizeof (tz));
59*2c51c3e4Skarels 	return (error);
60ef678427Sroot }
61ef678427Sroot 
62d1ec48c2Skarels settimeofday(p, uap, retval)
63d1ec48c2Skarels 	struct proc *p;
64d1ec48c2Skarels 	struct args {
65ef678427Sroot 		struct	timeval *tv;
66ef678427Sroot 		struct	timezone *tzp;
67d1ec48c2Skarels 	} *uap;
68d1ec48c2Skarels 	int *retval;
69d1ec48c2Skarels {
70ef678427Sroot 	struct timeval atv;
71ef678427Sroot 	struct timezone atz;
72d1ec48c2Skarels 	int error, s;
73ef678427Sroot 
74d1ec48c2Skarels 	if (error = suser(u.u_cred, &u.u_acflag))
75*2c51c3e4Skarels 		return (error);
762da4521eSbostic 	if (uap->tv) {
77d1ec48c2Skarels 		if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
78d1ec48c2Skarels 		    sizeof (struct timeval)))
79*2c51c3e4Skarels 			return (error);
80014695a9Smckusick 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
81014695a9Smckusick 		boottime.tv_sec += atv.tv_sec - time.tv_sec;
82014695a9Smckusick 		s = splhigh(); time = atv; splx(s);
83014695a9Smckusick 		resettodr();
842da4521eSbostic 	}
85d1ec48c2Skarels 	if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
86d1ec48c2Skarels 	    sizeof (atz))) == 0)
870228495eSsam 		tz = atz;
88*2c51c3e4Skarels 	return (error);
89ef678427Sroot }
9072762883Sroot 
91a690062aSkarels extern	int tickadj;			/* "standard" clock skew, us./tick */
92a690062aSkarels int	tickdelta;			/* current clock skew, us. per tick */
93a690062aSkarels long	timedelta;			/* unapplied time correction, us. */
94a690062aSkarels long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
955d3ec6cbSkarels 
96d1ec48c2Skarels /* ARGSUSED */
97d1ec48c2Skarels adjtime(p, uap, retval)
98d1ec48c2Skarels 	struct proc *p;
99d1ec48c2Skarels 	register struct args {
1005d3ec6cbSkarels 		struct timeval *delta;
1015d3ec6cbSkarels 		struct timeval *olddelta;
102d1ec48c2Skarels 	} *uap;
103d1ec48c2Skarels 	int *retval;
104d1ec48c2Skarels {
1055d3ec6cbSkarels 	struct timeval atv, oatv;
106a690062aSkarels 	register long ndelta;
107d1ec48c2Skarels 	int s, error;
1085d3ec6cbSkarels 
109d1ec48c2Skarels 	if (error = suser(u.u_cred, &u.u_acflag))
110*2c51c3e4Skarels 		return (error);
111d1ec48c2Skarels 	if (error =
112d1ec48c2Skarels 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
113*2c51c3e4Skarels 		return (error);
114a690062aSkarels 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
115a690062aSkarels 	if (timedelta == 0)
116a690062aSkarels 		if (ndelta > bigadj)
117a690062aSkarels 			tickdelta = 10 * tickadj;
118a690062aSkarels 		else
119a690062aSkarels 			tickdelta = tickadj;
120a690062aSkarels 	if (ndelta % tickdelta)
121a690062aSkarels 		ndelta = ndelta / tickadj * tickadj;
122a690062aSkarels 
123e782b972Skarels 	s = splclock();
1245d3ec6cbSkarels 	if (uap->olddelta) {
125a690062aSkarels 		oatv.tv_sec = timedelta / 1000000;
126a690062aSkarels 		oatv.tv_usec = timedelta % 1000000;
127a690062aSkarels 	}
128a690062aSkarels 	timedelta = ndelta;
129a690062aSkarels 	splx(s);
130a690062aSkarels 
131a690062aSkarels 	if (uap->olddelta)
1325d3ec6cbSkarels 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
1335d3ec6cbSkarels 			sizeof (struct timeval));
134*2c51c3e4Skarels 	return (0);
1355d3ec6cbSkarels }
1365d3ec6cbSkarels 
13734eef8ebSroot /*
13834eef8ebSroot  * Get value of an interval timer.  The process virtual and
13934eef8ebSroot  * profiling virtual time timers are kept in the u. area, since
14034eef8ebSroot  * they can be swapped out.  These are kept internally in the
14134eef8ebSroot  * way they are specified externally: in time until they expire.
14234eef8ebSroot  *
14334eef8ebSroot  * The real time interval timer is kept in the process table slot
14434eef8ebSroot  * for the process, and its value (it_value) is kept as an
14534eef8ebSroot  * absolute time rather than as a delta, so that it is easy to keep
14634eef8ebSroot  * periodic real-time signals from drifting.
14734eef8ebSroot  *
14834eef8ebSroot  * Virtual time timers are processed in the hardclock() routine of
14934eef8ebSroot  * kern_clock.c.  The real time timer is processed by a timeout
15034eef8ebSroot  * routine, called from the softclock() routine.  Since a callout
15134eef8ebSroot  * may be delayed in real time due to interrupt processing in the system,
15234eef8ebSroot  * it is possible for the real time timeout routine (realitexpire, given below),
15334eef8ebSroot  * to be delayed in real time past when it is supposed to occur.  It
15434eef8ebSroot  * does not suffice, therefore, to reload the real timer .it_value from the
15534eef8ebSroot  * real time timers .it_interval.  Rather, we compute the next time in
15634eef8ebSroot  * absolute time the timer should go off.
15734eef8ebSroot  */
158d1ec48c2Skarels /* ARGSUSED */
159d1ec48c2Skarels getitimer(p, uap, retval)
160d1ec48c2Skarels 	struct proc *p;
161d1ec48c2Skarels 	register struct args {
162ef678427Sroot 		u_int	which;
163ef678427Sroot 		struct	itimerval *itv;
164d1ec48c2Skarels 	} *uap;
165d1ec48c2Skarels 	int *retval;
166d1ec48c2Skarels {
1670ed3b3c8Sroot 	struct itimerval aitv;
168ef678427Sroot 	int s;
169ef678427Sroot 
170d1ec48c2Skarels 	if (uap->which > ITIMER_PROF)
171*2c51c3e4Skarels 		return (EINVAL);
17270dbe469Skarels 	s = splclock();
1730ed3b3c8Sroot 	if (uap->which == ITIMER_REAL) {
17434eef8ebSroot 		/*
17534eef8ebSroot 		 * Convert from absoulte to relative time in .it_value
17634eef8ebSroot 		 * part of real time timer.  If time for real time timer
17734eef8ebSroot 		 * has passed return 0, else return difference between
17834eef8ebSroot 		 * current time and time for the timer to go off.
17934eef8ebSroot 		 */
180d1ec48c2Skarels 		aitv = p->p_realtimer;
1810ed3b3c8Sroot 		if (timerisset(&aitv.it_value))
1820ed3b3c8Sroot 			if (timercmp(&aitv.it_value, &time, <))
1830ed3b3c8Sroot 				timerclear(&aitv.it_value);
1840ed3b3c8Sroot 			else
1850ed3b3c8Sroot 				timevalsub(&aitv.it_value, &time);
1860ed3b3c8Sroot 	} else
1870ed3b3c8Sroot 		aitv = u.u_timer[uap->which];
1880ed3b3c8Sroot 	splx(s);
189*2c51c3e4Skarels 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
190d1ec48c2Skarels 	    sizeof (struct itimerval)));
191ef678427Sroot }
192ef678427Sroot 
193d1ec48c2Skarels /* ARGSUSED */
194d1ec48c2Skarels setitimer(p, uap, retval)
195d1ec48c2Skarels 	struct proc *p;
196d1ec48c2Skarels 	register struct args {
197ef678427Sroot 		u_int	which;
19872762883Sroot 		struct	itimerval *itv, *oitv;
199d1ec48c2Skarels 	} *uap;
200d1ec48c2Skarels 	int *retval;
201d1ec48c2Skarels {
2023468c2f1Smckusick 	struct itimerval aitv;
2033468c2f1Smckusick 	register struct itimerval *itvp;
204d1ec48c2Skarels 	int s, error;
205ef678427Sroot 
206d1ec48c2Skarels 	if (uap->which > ITIMER_PROF)
207*2c51c3e4Skarels 		return (EINVAL);
2083468c2f1Smckusick 	itvp = uap->itv;
209d1ec48c2Skarels 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
2103468c2f1Smckusick 	    sizeof(struct itimerval))))
211*2c51c3e4Skarels 		return (error);
212d1ec48c2Skarels 	if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
213*2c51c3e4Skarels 		return (error);
2143468c2f1Smckusick 	if (itvp == 0)
215d1ec48c2Skarels 		return (0);
216d1ec48c2Skarels 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
217*2c51c3e4Skarels 		return (EINVAL);
21870dbe469Skarels 	s = splclock();
2190ed3b3c8Sroot 	if (uap->which == ITIMER_REAL) {
220a11faa3dSroot 		untimeout(realitexpire, (caddr_t)p);
2210ed3b3c8Sroot 		if (timerisset(&aitv.it_value)) {
2220ed3b3c8Sroot 			timevaladd(&aitv.it_value, &time);
223a11faa3dSroot 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
2240ed3b3c8Sroot 		}
2250ed3b3c8Sroot 		p->p_realtimer = aitv;
2260ed3b3c8Sroot 	} else
22772762883Sroot 		u.u_timer[uap->which] = aitv;
228ef678427Sroot 	splx(s);
229*2c51c3e4Skarels 	return (0);
230ef678427Sroot }
231ef678427Sroot 
23234eef8ebSroot /*
23334eef8ebSroot  * Real interval timer expired:
23434eef8ebSroot  * send process whose timer expired an alarm signal.
23534eef8ebSroot  * If time is not set up to reload, then just return.
23634eef8ebSroot  * Else compute next time timer should go off which is > current time.
23734eef8ebSroot  * This is where delay in processing this timeout causes multiple
23834eef8ebSroot  * SIGALRM calls to be compressed into one.
23934eef8ebSroot  */
24034eef8ebSroot realitexpire(p)
2410ed3b3c8Sroot 	register struct proc *p;
2420ed3b3c8Sroot {
2430ed3b3c8Sroot 	int s;
2440ed3b3c8Sroot 
2450ed3b3c8Sroot 	psignal(p, SIGALRM);
2460ed3b3c8Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
2470ed3b3c8Sroot 		timerclear(&p->p_realtimer.it_value);
2480ed3b3c8Sroot 		return;
2490ed3b3c8Sroot 	}
2500ed3b3c8Sroot 	for (;;) {
25170dbe469Skarels 		s = splclock();
2520ed3b3c8Sroot 		timevaladd(&p->p_realtimer.it_value,
2530ed3b3c8Sroot 		    &p->p_realtimer.it_interval);
2540ed3b3c8Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
255a11faa3dSroot 			timeout(realitexpire, (caddr_t)p,
256a11faa3dSroot 			    hzto(&p->p_realtimer.it_value));
2570ed3b3c8Sroot 			splx(s);
2580ed3b3c8Sroot 			return;
2590ed3b3c8Sroot 		}
2600ed3b3c8Sroot 		splx(s);
2610ed3b3c8Sroot 	}
2620ed3b3c8Sroot }
2630ed3b3c8Sroot 
26434eef8ebSroot /*
26534eef8ebSroot  * Check that a proposed value to load into the .it_value or
26634eef8ebSroot  * .it_interval part of an interval timer is acceptable, and
26734eef8ebSroot  * fix it to have at least minimal value (i.e. if it is less
26834eef8ebSroot  * than the resolution of the clock, round it up.)
26934eef8ebSroot  */
27072762883Sroot itimerfix(tv)
27172762883Sroot 	struct timeval *tv;
27272762883Sroot {
27372762883Sroot 
2740ed3b3c8Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2750ed3b3c8Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
27672762883Sroot 		return (EINVAL);
27742312cf9Ssam 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
27872762883Sroot 		tv->tv_usec = tick;
27972762883Sroot 	return (0);
28072762883Sroot }
28172762883Sroot 
28234eef8ebSroot /*
28334eef8ebSroot  * Decrement an interval timer by a specified number
28434eef8ebSroot  * of microseconds, which must be less than a second,
28534eef8ebSroot  * i.e. < 1000000.  If the timer expires, then reload
28634eef8ebSroot  * it.  In this case, carry over (usec - old value) to
28734eef8ebSroot  * reducint the value reloaded into the timer so that
28834eef8ebSroot  * the timer does not drift.  This routine assumes
28934eef8ebSroot  * that it is called in a context where the timers
29034eef8ebSroot  * on which it is operating cannot change in value.
29134eef8ebSroot  */
292ef678427Sroot itimerdecr(itp, usec)
293ef678427Sroot 	register struct itimerval *itp;
294ef678427Sroot 	int usec;
295ef678427Sroot {
296ef678427Sroot 
29772762883Sroot 	if (itp->it_value.tv_usec < usec) {
29872762883Sroot 		if (itp->it_value.tv_sec == 0) {
29934eef8ebSroot 			/* expired, and already in next interval */
30072762883Sroot 			usec -= itp->it_value.tv_usec;
301ef678427Sroot 			goto expire;
302ef678427Sroot 		}
30372762883Sroot 		itp->it_value.tv_usec += 1000000;
30472762883Sroot 		itp->it_value.tv_sec--;
30572762883Sroot 	}
30672762883Sroot 	itp->it_value.tv_usec -= usec;
30772762883Sroot 	usec = 0;
30872762883Sroot 	if (timerisset(&itp->it_value))
309ef678427Sroot 		return (1);
31034eef8ebSroot 	/* expired, exactly at end of interval */
311ef678427Sroot expire:
31272762883Sroot 	if (timerisset(&itp->it_interval)) {
31372762883Sroot 		itp->it_value = itp->it_interval;
31472762883Sroot 		itp->it_value.tv_usec -= usec;
31572762883Sroot 		if (itp->it_value.tv_usec < 0) {
31672762883Sroot 			itp->it_value.tv_usec += 1000000;
31772762883Sroot 			itp->it_value.tv_sec--;
31872762883Sroot 		}
31972762883Sroot 	} else
32034eef8ebSroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
321ef678427Sroot 	return (0);
322ef678427Sroot }
323ef678427Sroot 
32434eef8ebSroot /*
32534eef8ebSroot  * Add and subtract routines for timevals.
32634eef8ebSroot  * N.B.: subtract routine doesn't deal with
32734eef8ebSroot  * results which are before the beginning,
32834eef8ebSroot  * it just gets very confused in this case.
32934eef8ebSroot  * Caveat emptor.
33034eef8ebSroot  */
33134eef8ebSroot timevaladd(t1, t2)
33234eef8ebSroot 	struct timeval *t1, *t2;
33334eef8ebSroot {
33434eef8ebSroot 
33534eef8ebSroot 	t1->tv_sec += t2->tv_sec;
33634eef8ebSroot 	t1->tv_usec += t2->tv_usec;
33734eef8ebSroot 	timevalfix(t1);
33834eef8ebSroot }
33934eef8ebSroot 
34034eef8ebSroot timevalsub(t1, t2)
34134eef8ebSroot 	struct timeval *t1, *t2;
34234eef8ebSroot {
34334eef8ebSroot 
34434eef8ebSroot 	t1->tv_sec -= t2->tv_sec;
34534eef8ebSroot 	t1->tv_usec -= t2->tv_usec;
34634eef8ebSroot 	timevalfix(t1);
34734eef8ebSroot }
34834eef8ebSroot 
34934eef8ebSroot timevalfix(t1)
35034eef8ebSroot 	struct timeval *t1;
35134eef8ebSroot {
35234eef8ebSroot 
35334eef8ebSroot 	if (t1->tv_usec < 0) {
35434eef8ebSroot 		t1->tv_sec--;
35534eef8ebSroot 		t1->tv_usec += 1000000;
35634eef8ebSroot 	}
35734eef8ebSroot 	if (t1->tv_usec >= 1000000) {
35834eef8ebSroot 		t1->tv_sec++;
35934eef8ebSroot 		t1->tv_usec -= 1000000;
36034eef8ebSroot 	}
36134eef8ebSroot }
362