xref: /csrg-svn/sys/kern/kern_time.c (revision 25227)
123377Smckusick /*
223377Smckusick  * Copyright (c) 1982 Regents of the University of California.
323377Smckusick  * All rights reserved.  The Berkeley software License Agreement
423377Smckusick  * specifies the terms and conditions for redistribution.
523377Smckusick  *
6*25227Smckusick  *	@(#)kern_time.c	6.7 (Berkeley) 10/17/85
723377Smckusick  */
87424Sroot 
99757Ssam #include "../machine/reg.h"
109757Ssam 
1117093Sbloom #include "param.h"
1217093Sbloom #include "dir.h"		/* XXX */
1317093Sbloom #include "user.h"
1417093Sbloom #include "kernel.h"
1517093Sbloom #include "inode.h"
1617093Sbloom #include "proc.h"
177424Sroot 
188103Sroot /*
198103Sroot  * Time of day and interval timer support.
208146Sroot  *
218146Sroot  * These routines provide the kernel entry points to get and set
228146Sroot  * the time-of-day and per-process interval timers.  Subroutines
238146Sroot  * here provide support for adding and subtracting timeval structures
248146Sroot  * and decrementing interval timers, optionally reloading the interval
258146Sroot  * timers when they expire.
268103Sroot  */
278103Sroot 
288034Sroot gettimeofday()
297424Sroot {
308034Sroot 	register struct a {
318034Sroot 		struct	timeval *tp;
328034Sroot 		struct	timezone *tzp;
338034Sroot 	} *uap = (struct a *)u.u_ap;
348034Sroot 	struct timeval atv;
358103Sroot 	int s;
367500Sroot 
378103Sroot 	s = spl7(); atv = time; splx(s);
389998Ssam 	u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv));
399998Ssam 	if (u.u_error)
408034Sroot 		return;
418034Sroot 	if (uap->tzp == 0)
428034Sroot 		return;
438103Sroot 	/* SHOULD HAVE PER-PROCESS TIMEZONE */
449998Ssam 	u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, sizeof (tz));
457500Sroot }
467500Sroot 
478034Sroot settimeofday()
487500Sroot {
498034Sroot 	register struct a {
508103Sroot 		struct	timeval *tv;
518103Sroot 		struct	timezone *tzp;
528034Sroot 	} *uap = (struct a *)u.u_ap;
538034Sroot 	struct timeval atv;
548034Sroot 	struct timezone atz;
557500Sroot 
569998Ssam 	u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
579998Ssam 		sizeof (struct timeval));
589998Ssam 	if (u.u_error)
598034Sroot 		return;
608103Sroot 	setthetime(&atv);
618103Sroot 	if (uap->tzp && suser()) {
629998Ssam 		u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
639998Ssam 			sizeof (atz));
6416576Ssam 		if (u.u_error == 0)
6516576Ssam 			tz = atz;
668034Sroot 	}
677500Sroot }
687500Sroot 
698103Sroot setthetime(tv)
708103Sroot 	struct timeval *tv;
718103Sroot {
728103Sroot 	int s;
738103Sroot 
748103Sroot 	if (!suser())
758103Sroot 		return;
768146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
778103Sroot 	boottime.tv_sec += tv->tv_sec - time.tv_sec;
788103Sroot 	s = spl7(); time = *tv; splx(s);
799007Sroot 	resettodr();
808103Sroot }
818103Sroot 
8217356Skarels int adjtimedelta;
8325170Skarels extern int tickadj;
8417356Skarels 
8517356Skarels adjtime()
8617356Skarels {
8717356Skarels 	register struct a {
8817356Skarels 		struct timeval *delta;
8917356Skarels 		struct timeval *olddelta;
9017356Skarels 	} *uap = (struct a *)u.u_ap;
9117356Skarels 	struct timeval atv, oatv;
9225170Skarels 	int s;
9317356Skarels 
9417356Skarels 	if (!suser())
9517356Skarels 		return;
9617356Skarels 	u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv,
9717356Skarels 		sizeof (struct timeval));
9817356Skarels 	if (u.u_error)
9917356Skarels 		return;
10025170Skarels 	s = splclock();
10117356Skarels 	if (uap->olddelta) {
10217356Skarels 		oatv.tv_sec = adjtimedelta / 1000000;
10317356Skarels 		oatv.tv_usec = adjtimedelta % 1000000;
10417356Skarels 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
10517356Skarels 			sizeof (struct timeval));
10617356Skarels 	}
10717356Skarels 	adjtimedelta = atv.tv_sec * 1000000 + atv.tv_usec;
10825170Skarels 	if (adjtimedelta % tickadj)
10925170Skarels 		adjtimedelta = adjtimedelta / tickadj * tickadj;
11025170Skarels 	splx(s);
11117356Skarels }
11217356Skarels 
1138146Sroot /*
1148146Sroot  * Get value of an interval timer.  The process virtual and
1158146Sroot  * profiling virtual time timers are kept in the u. area, since
1168146Sroot  * they can be swapped out.  These are kept internally in the
1178146Sroot  * way they are specified externally: in time until they expire.
1188146Sroot  *
1198146Sroot  * The real time interval timer is kept in the process table slot
1208146Sroot  * for the process, and its value (it_value) is kept as an
1218146Sroot  * absolute time rather than as a delta, so that it is easy to keep
1228146Sroot  * periodic real-time signals from drifting.
1238146Sroot  *
1248146Sroot  * Virtual time timers are processed in the hardclock() routine of
1258146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
1268146Sroot  * routine, called from the softclock() routine.  Since a callout
1278146Sroot  * may be delayed in real time due to interrupt processing in the system,
1288146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
1298146Sroot  * to be delayed in real time past when it is supposed to occur.  It
1308146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
1318146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
1328146Sroot  * absolute time the timer should go off.
1338146Sroot  */
1348034Sroot getitimer()
1358034Sroot {
1367424Sroot 	register struct a {
1378034Sroot 		u_int	which;
1388034Sroot 		struct	itimerval *itv;
1398034Sroot 	} *uap = (struct a *)u.u_ap;
1408114Sroot 	struct itimerval aitv;
1418034Sroot 	int s;
1427424Sroot 
1438034Sroot 	if (uap->which > 2) {
1448034Sroot 		u.u_error = EINVAL;
1458034Sroot 		return;
1467424Sroot 	}
1478034Sroot 	s = spl7();
1488114Sroot 	if (uap->which == ITIMER_REAL) {
1498146Sroot 		/*
1508146Sroot 		 * Convert from absoulte to relative time in .it_value
1518146Sroot 		 * part of real time timer.  If time for real time timer
1528146Sroot 		 * has passed return 0, else return difference between
1538146Sroot 		 * current time and time for the timer to go off.
1548146Sroot 		 */
1558114Sroot 		aitv = u.u_procp->p_realtimer;
1568114Sroot 		if (timerisset(&aitv.it_value))
1578114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1588114Sroot 				timerclear(&aitv.it_value);
1598114Sroot 			else
1608114Sroot 				timevalsub(&aitv.it_value, &time);
1618114Sroot 	} else
1628114Sroot 		aitv = u.u_timer[uap->which];
1638114Sroot 	splx(s);
1649998Ssam 	u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
1659998Ssam 	    sizeof (struct itimerval));
1668034Sroot 	splx(s);
1677424Sroot }
1687424Sroot 
1698034Sroot setitimer()
1707424Sroot {
1717424Sroot 	register struct a {
1728034Sroot 		u_int	which;
1738103Sroot 		struct	itimerval *itv, *oitv;
1748034Sroot 	} *uap = (struct a *)u.u_ap;
175*25227Smckusick 	struct itimerval aitv, *aitvp;
1768034Sroot 	int s;
1778114Sroot 	register struct proc *p = u.u_procp;
1787424Sroot 
1798034Sroot 	if (uap->which > 2) {
1808034Sroot 		u.u_error = EINVAL;
1818103Sroot 		return;
1827424Sroot 	}
183*25227Smckusick 	aitvp = uap->itv;
1848103Sroot 	if (uap->oitv) {
1858103Sroot 		uap->itv = uap->oitv;
1868103Sroot 		getitimer();
1878103Sroot 	}
188*25227Smckusick 	if (aitvp == 0)
189*25227Smckusick 		return;
190*25227Smckusick 	u.u_error = copyin(aitvp, (caddr_t)&aitv, sizeof (struct itimerval));
191*25227Smckusick 	if (u.u_error)
192*25227Smckusick 		return;
1938103Sroot 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) {
1948103Sroot 		u.u_error = EINVAL;
1958103Sroot 		return;
1968103Sroot 	}
1978103Sroot 	s = spl7();
1988114Sroot 	if (uap->which == ITIMER_REAL) {
1998625Sroot 		untimeout(realitexpire, (caddr_t)p);
2008114Sroot 		if (timerisset(&aitv.it_value)) {
2018114Sroot 			timevaladd(&aitv.it_value, &time);
2028625Sroot 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
2038114Sroot 		}
2048114Sroot 		p->p_realtimer = aitv;
2058114Sroot 	} else
2068103Sroot 		u.u_timer[uap->which] = aitv;
2078034Sroot 	splx(s);
2087424Sroot }
2097424Sroot 
2108146Sroot /*
2118146Sroot  * Real interval timer expired:
2128146Sroot  * send process whose timer expired an alarm signal.
2138146Sroot  * If time is not set up to reload, then just return.
2148146Sroot  * Else compute next time timer should go off which is > current time.
2158146Sroot  * This is where delay in processing this timeout causes multiple
2168146Sroot  * SIGALRM calls to be compressed into one.
2178146Sroot  */
2188146Sroot realitexpire(p)
2198114Sroot 	register struct proc *p;
2208114Sroot {
2218114Sroot 	int s;
2228114Sroot 
2238114Sroot 	psignal(p, SIGALRM);
2248114Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
2258114Sroot 		timerclear(&p->p_realtimer.it_value);
2268114Sroot 		return;
2278114Sroot 	}
2288114Sroot 	for (;;) {
2298114Sroot 		s = spl7();
2308114Sroot 		timevaladd(&p->p_realtimer.it_value,
2318114Sroot 		    &p->p_realtimer.it_interval);
2328114Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
2338625Sroot 			timeout(realitexpire, (caddr_t)p,
2348625Sroot 			    hzto(&p->p_realtimer.it_value));
2358114Sroot 			splx(s);
2368114Sroot 			return;
2378114Sroot 		}
2388114Sroot 		splx(s);
2398114Sroot 	}
2408114Sroot }
2418114Sroot 
2428146Sroot /*
2438146Sroot  * Check that a proposed value to load into the .it_value or
2448146Sroot  * .it_interval part of an interval timer is acceptable, and
2458146Sroot  * fix it to have at least minimal value (i.e. if it is less
2468146Sroot  * than the resolution of the clock, round it up.)
2478146Sroot  */
2488103Sroot itimerfix(tv)
2498103Sroot 	struct timeval *tv;
2507424Sroot {
2518034Sroot 
2528114Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2538114Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
2548103Sroot 		return (EINVAL);
25512970Ssam 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
2568103Sroot 		tv->tv_usec = tick;
2578103Sroot 	return (0);
2588034Sroot }
2598034Sroot 
2608146Sroot /*
2618146Sroot  * Decrement an interval timer by a specified number
2628146Sroot  * of microseconds, which must be less than a second,
2638146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2648146Sroot  * it.  In this case, carry over (usec - old value) to
2658146Sroot  * reducint the value reloaded into the timer so that
2668146Sroot  * the timer does not drift.  This routine assumes
2678146Sroot  * that it is called in a context where the timers
2688146Sroot  * on which it is operating cannot change in value.
2698146Sroot  */
2708034Sroot itimerdecr(itp, usec)
2718034Sroot 	register struct itimerval *itp;
2728034Sroot 	int usec;
2738034Sroot {
2748034Sroot 
2758103Sroot 	if (itp->it_value.tv_usec < usec) {
2768103Sroot 		if (itp->it_value.tv_sec == 0) {
2778146Sroot 			/* expired, and already in next interval */
2788103Sroot 			usec -= itp->it_value.tv_usec;
2798034Sroot 			goto expire;
2808103Sroot 		}
2818103Sroot 		itp->it_value.tv_usec += 1000000;
2828103Sroot 		itp->it_value.tv_sec--;
2838034Sroot 	}
2848103Sroot 	itp->it_value.tv_usec -= usec;
2858103Sroot 	usec = 0;
2868103Sroot 	if (timerisset(&itp->it_value))
2878034Sroot 		return (1);
2888146Sroot 	/* expired, exactly at end of interval */
2898034Sroot expire:
2908103Sroot 	if (timerisset(&itp->it_interval)) {
2918103Sroot 		itp->it_value = itp->it_interval;
2928103Sroot 		itp->it_value.tv_usec -= usec;
2938103Sroot 		if (itp->it_value.tv_usec < 0) {
2948103Sroot 			itp->it_value.tv_usec += 1000000;
2958103Sroot 			itp->it_value.tv_sec--;
2968103Sroot 		}
2978103Sroot 	} else
2988146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
2998034Sroot 	return (0);
3008034Sroot }
3018034Sroot 
3028146Sroot /*
3038146Sroot  * Add and subtract routines for timevals.
3048146Sroot  * N.B.: subtract routine doesn't deal with
3058146Sroot  * results which are before the beginning,
3068146Sroot  * it just gets very confused in this case.
3078146Sroot  * Caveat emptor.
3088146Sroot  */
3098146Sroot timevaladd(t1, t2)
3108146Sroot 	struct timeval *t1, *t2;
3118146Sroot {
3128146Sroot 
3138146Sroot 	t1->tv_sec += t2->tv_sec;
3148146Sroot 	t1->tv_usec += t2->tv_usec;
3158146Sroot 	timevalfix(t1);
3168146Sroot }
3178146Sroot 
3188146Sroot timevalsub(t1, t2)
3198146Sroot 	struct timeval *t1, *t2;
3208146Sroot {
3218146Sroot 
3228146Sroot 	t1->tv_sec -= t2->tv_sec;
3238146Sroot 	t1->tv_usec -= t2->tv_usec;
3248146Sroot 	timevalfix(t1);
3258146Sroot }
3268146Sroot 
3278146Sroot timevalfix(t1)
3288146Sroot 	struct timeval *t1;
3298146Sroot {
3308146Sroot 
3318146Sroot 	if (t1->tv_usec < 0) {
3328146Sroot 		t1->tv_sec--;
3338146Sroot 		t1->tv_usec += 1000000;
3348146Sroot 	}
3358146Sroot 	if (t1->tv_usec >= 1000000) {
3368146Sroot 		t1->tv_sec++;
3378146Sroot 		t1->tv_usec -= 1000000;
3388146Sroot 	}
3398146Sroot }
340