xref: /csrg-svn/sys/kern/kern_time.c (revision 23377)
1*23377Smckusick /*
2*23377Smckusick  * Copyright (c) 1982 Regents of the University of California.
3*23377Smckusick  * All rights reserved.  The Berkeley software License Agreement
4*23377Smckusick  * specifies the terms and conditions for redistribution.
5*23377Smckusick  *
6*23377Smckusick  *	@(#)kern_time.c	6.5 (Berkeley) 06/08/85
7*23377Smckusick  */
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;
8317356Skarels 
8417356Skarels adjtime()
8517356Skarels {
8617356Skarels 	register struct a {
8717356Skarels 		struct timeval *delta;
8817356Skarels 		struct timeval *olddelta;
8917356Skarels 	} *uap = (struct a *)u.u_ap;
9017356Skarels 
9117356Skarels 	struct timeval atv, oatv;
9217356Skarels 
9317356Skarels 	if (!suser())
9417356Skarels 		return;
9517356Skarels 	u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv,
9617356Skarels 		sizeof (struct timeval));
9717356Skarels 	if (u.u_error)
9817356Skarels 		return;
9917356Skarels 	if (uap->olddelta) {
10017356Skarels 		oatv.tv_sec = adjtimedelta / 1000000;
10117356Skarels 		oatv.tv_usec = adjtimedelta % 1000000;
10217356Skarels 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
10317356Skarels 			sizeof (struct timeval));
10417356Skarels 	}
10517356Skarels 	adjtimedelta = atv.tv_sec * 1000000 + atv.tv_usec;
10617356Skarels }
10717356Skarels 
1088146Sroot /*
1098146Sroot  * Get value of an interval timer.  The process virtual and
1108146Sroot  * profiling virtual time timers are kept in the u. area, since
1118146Sroot  * they can be swapped out.  These are kept internally in the
1128146Sroot  * way they are specified externally: in time until they expire.
1138146Sroot  *
1148146Sroot  * The real time interval timer is kept in the process table slot
1158146Sroot  * for the process, and its value (it_value) is kept as an
1168146Sroot  * absolute time rather than as a delta, so that it is easy to keep
1178146Sroot  * periodic real-time signals from drifting.
1188146Sroot  *
1198146Sroot  * Virtual time timers are processed in the hardclock() routine of
1208146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
1218146Sroot  * routine, called from the softclock() routine.  Since a callout
1228146Sroot  * may be delayed in real time due to interrupt processing in the system,
1238146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
1248146Sroot  * to be delayed in real time past when it is supposed to occur.  It
1258146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
1268146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
1278146Sroot  * absolute time the timer should go off.
1288146Sroot  */
1298034Sroot getitimer()
1308034Sroot {
1317424Sroot 	register struct a {
1328034Sroot 		u_int	which;
1338034Sroot 		struct	itimerval *itv;
1348034Sroot 	} *uap = (struct a *)u.u_ap;
1358114Sroot 	struct itimerval aitv;
1368034Sroot 	int s;
1377424Sroot 
1388034Sroot 	if (uap->which > 2) {
1398034Sroot 		u.u_error = EINVAL;
1408034Sroot 		return;
1417424Sroot 	}
1428034Sroot 	s = spl7();
1438114Sroot 	if (uap->which == ITIMER_REAL) {
1448146Sroot 		/*
1458146Sroot 		 * Convert from absoulte to relative time in .it_value
1468146Sroot 		 * part of real time timer.  If time for real time timer
1478146Sroot 		 * has passed return 0, else return difference between
1488146Sroot 		 * current time and time for the timer to go off.
1498146Sroot 		 */
1508114Sroot 		aitv = u.u_procp->p_realtimer;
1518114Sroot 		if (timerisset(&aitv.it_value))
1528114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1538114Sroot 				timerclear(&aitv.it_value);
1548114Sroot 			else
1558114Sroot 				timevalsub(&aitv.it_value, &time);
1568114Sroot 	} else
1578114Sroot 		aitv = u.u_timer[uap->which];
1588114Sroot 	splx(s);
1599998Ssam 	u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
1609998Ssam 	    sizeof (struct itimerval));
1618034Sroot 	splx(s);
1627424Sroot }
1637424Sroot 
1648034Sroot setitimer()
1657424Sroot {
1667424Sroot 	register struct a {
1678034Sroot 		u_int	which;
1688103Sroot 		struct	itimerval *itv, *oitv;
1698034Sroot 	} *uap = (struct a *)u.u_ap;
1708034Sroot 	struct itimerval aitv;
1718034Sroot 	int s;
1728114Sroot 	register struct proc *p = u.u_procp;
1737424Sroot 
1748034Sroot 	if (uap->which > 2) {
1758034Sroot 		u.u_error = EINVAL;
1768103Sroot 		return;
1777424Sroot 	}
1789998Ssam 	u.u_error = copyin((caddr_t)uap->itv, (caddr_t)&aitv,
1799998Ssam 	    sizeof (struct itimerval));
1809998Ssam 	if (u.u_error)
1818103Sroot 		return;
1828103Sroot 	if (uap->oitv) {
1838103Sroot 		uap->itv = uap->oitv;
1848103Sroot 		getitimer();
1858103Sroot 	}
1868103Sroot 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) {
1878103Sroot 		u.u_error = EINVAL;
1888103Sroot 		return;
1898103Sroot 	}
1908103Sroot 	s = spl7();
1918114Sroot 	if (uap->which == ITIMER_REAL) {
1928625Sroot 		untimeout(realitexpire, (caddr_t)p);
1938114Sroot 		if (timerisset(&aitv.it_value)) {
1948114Sroot 			timevaladd(&aitv.it_value, &time);
1958625Sroot 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
1968114Sroot 		}
1978114Sroot 		p->p_realtimer = aitv;
1988114Sroot 	} else
1998103Sroot 		u.u_timer[uap->which] = aitv;
2008034Sroot 	splx(s);
2017424Sroot }
2027424Sroot 
2038146Sroot /*
2048146Sroot  * Real interval timer expired:
2058146Sroot  * send process whose timer expired an alarm signal.
2068146Sroot  * If time is not set up to reload, then just return.
2078146Sroot  * Else compute next time timer should go off which is > current time.
2088146Sroot  * This is where delay in processing this timeout causes multiple
2098146Sroot  * SIGALRM calls to be compressed into one.
2108146Sroot  */
2118146Sroot realitexpire(p)
2128114Sroot 	register struct proc *p;
2138114Sroot {
2148114Sroot 	int s;
2158114Sroot 
2168114Sroot 	psignal(p, SIGALRM);
2178114Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
2188114Sroot 		timerclear(&p->p_realtimer.it_value);
2198114Sroot 		return;
2208114Sroot 	}
2218114Sroot 	for (;;) {
2228114Sroot 		s = spl7();
2238114Sroot 		timevaladd(&p->p_realtimer.it_value,
2248114Sroot 		    &p->p_realtimer.it_interval);
2258114Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
2268625Sroot 			timeout(realitexpire, (caddr_t)p,
2278625Sroot 			    hzto(&p->p_realtimer.it_value));
2288114Sroot 			splx(s);
2298114Sroot 			return;
2308114Sroot 		}
2318114Sroot 		splx(s);
2328114Sroot 	}
2338114Sroot }
2348114Sroot 
2358146Sroot /*
2368146Sroot  * Check that a proposed value to load into the .it_value or
2378146Sroot  * .it_interval part of an interval timer is acceptable, and
2388146Sroot  * fix it to have at least minimal value (i.e. if it is less
2398146Sroot  * than the resolution of the clock, round it up.)
2408146Sroot  */
2418103Sroot itimerfix(tv)
2428103Sroot 	struct timeval *tv;
2437424Sroot {
2448034Sroot 
2458114Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2468114Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
2478103Sroot 		return (EINVAL);
24812970Ssam 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
2498103Sroot 		tv->tv_usec = tick;
2508103Sroot 	return (0);
2518034Sroot }
2528034Sroot 
2538146Sroot /*
2548146Sroot  * Decrement an interval timer by a specified number
2558146Sroot  * of microseconds, which must be less than a second,
2568146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2578146Sroot  * it.  In this case, carry over (usec - old value) to
2588146Sroot  * reducint the value reloaded into the timer so that
2598146Sroot  * the timer does not drift.  This routine assumes
2608146Sroot  * that it is called in a context where the timers
2618146Sroot  * on which it is operating cannot change in value.
2628146Sroot  */
2638034Sroot itimerdecr(itp, usec)
2648034Sroot 	register struct itimerval *itp;
2658034Sroot 	int usec;
2668034Sroot {
2678034Sroot 
2688103Sroot 	if (itp->it_value.tv_usec < usec) {
2698103Sroot 		if (itp->it_value.tv_sec == 0) {
2708146Sroot 			/* expired, and already in next interval */
2718103Sroot 			usec -= itp->it_value.tv_usec;
2728034Sroot 			goto expire;
2738103Sroot 		}
2748103Sroot 		itp->it_value.tv_usec += 1000000;
2758103Sroot 		itp->it_value.tv_sec--;
2768034Sroot 	}
2778103Sroot 	itp->it_value.tv_usec -= usec;
2788103Sroot 	usec = 0;
2798103Sroot 	if (timerisset(&itp->it_value))
2808034Sroot 		return (1);
2818146Sroot 	/* expired, exactly at end of interval */
2828034Sroot expire:
2838103Sroot 	if (timerisset(&itp->it_interval)) {
2848103Sroot 		itp->it_value = itp->it_interval;
2858103Sroot 		itp->it_value.tv_usec -= usec;
2868103Sroot 		if (itp->it_value.tv_usec < 0) {
2878103Sroot 			itp->it_value.tv_usec += 1000000;
2888103Sroot 			itp->it_value.tv_sec--;
2898103Sroot 		}
2908103Sroot 	} else
2918146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
2928034Sroot 	return (0);
2938034Sroot }
2948034Sroot 
2958146Sroot /*
2968146Sroot  * Add and subtract routines for timevals.
2978146Sroot  * N.B.: subtract routine doesn't deal with
2988146Sroot  * results which are before the beginning,
2998146Sroot  * it just gets very confused in this case.
3008146Sroot  * Caveat emptor.
3018146Sroot  */
3028146Sroot timevaladd(t1, t2)
3038146Sroot 	struct timeval *t1, *t2;
3048146Sroot {
3058146Sroot 
3068146Sroot 	t1->tv_sec += t2->tv_sec;
3078146Sroot 	t1->tv_usec += t2->tv_usec;
3088146Sroot 	timevalfix(t1);
3098146Sroot }
3108146Sroot 
3118146Sroot timevalsub(t1, t2)
3128146Sroot 	struct timeval *t1, *t2;
3138146Sroot {
3148146Sroot 
3158146Sroot 	t1->tv_sec -= t2->tv_sec;
3168146Sroot 	t1->tv_usec -= t2->tv_usec;
3178146Sroot 	timevalfix(t1);
3188146Sroot }
3198146Sroot 
3208146Sroot timevalfix(t1)
3218146Sroot 	struct timeval *t1;
3228146Sroot {
3238146Sroot 
3248146Sroot 	if (t1->tv_usec < 0) {
3258146Sroot 		t1->tv_sec--;
3268146Sroot 		t1->tv_usec += 1000000;
3278146Sroot 	}
3288146Sroot 	if (t1->tv_usec >= 1000000) {
3298146Sroot 		t1->tv_sec++;
3308146Sroot 		t1->tv_usec -= 1000000;
3318146Sroot 	}
3328146Sroot }
333