xref: /csrg-svn/sys/kern/kern_time.c (revision 29097)
123377Smckusick /*
2*29097Smckusick  * Copyright (c) 1982, 1986 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*29097Smckusick  *	@(#)kern_time.c	7.1 (Berkeley) 06/05/86
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;
357500Sroot 
3625897Skarels 	microtime(&atv);
379998Ssam 	u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp, sizeof (atv));
389998Ssam 	if (u.u_error)
398034Sroot 		return;
408034Sroot 	if (uap->tzp == 0)
418034Sroot 		return;
428103Sroot 	/* SHOULD HAVE PER-PROCESS TIMEZONE */
439998Ssam 	u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp, sizeof (tz));
447500Sroot }
457500Sroot 
468034Sroot settimeofday()
477500Sroot {
488034Sroot 	register struct a {
498103Sroot 		struct	timeval *tv;
508103Sroot 		struct	timezone *tzp;
518034Sroot 	} *uap = (struct a *)u.u_ap;
528034Sroot 	struct timeval atv;
538034Sroot 	struct timezone atz;
547500Sroot 
559998Ssam 	u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
569998Ssam 		sizeof (struct timeval));
579998Ssam 	if (u.u_error)
588034Sroot 		return;
598103Sroot 	setthetime(&atv);
608103Sroot 	if (uap->tzp && suser()) {
619998Ssam 		u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
629998Ssam 			sizeof (atz));
6316576Ssam 		if (u.u_error == 0)
6416576Ssam 			tz = atz;
658034Sroot 	}
667500Sroot }
677500Sroot 
688103Sroot setthetime(tv)
698103Sroot 	struct timeval *tv;
708103Sroot {
718103Sroot 	int s;
728103Sroot 
738103Sroot 	if (!suser())
748103Sroot 		return;
758146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
768103Sroot 	boottime.tv_sec += tv->tv_sec - time.tv_sec;
7725897Skarels 	s = splhigh(); time = *tv; splx(s);
789007Sroot 	resettodr();
798103Sroot }
808103Sroot 
8128829Skarels extern	int tickadj;			/* "standard" clock skew, us./tick */
8228829Skarels int	tickdelta;			/* current clock skew, us. per tick */
8328829Skarels long	timedelta;			/* unapplied time correction, us. */
8428829Skarels long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
8517356Skarels 
8617356Skarels adjtime()
8717356Skarels {
8817356Skarels 	register struct a {
8917356Skarels 		struct timeval *delta;
9017356Skarels 		struct timeval *olddelta;
9117356Skarels 	} *uap = (struct a *)u.u_ap;
9217356Skarels 	struct timeval atv, oatv;
9328829Skarels 	register long ndelta;
9425170Skarels 	int s;
9517356Skarels 
9617356Skarels 	if (!suser())
9717356Skarels 		return;
9817356Skarels 	u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv,
9917356Skarels 		sizeof (struct timeval));
10017356Skarels 	if (u.u_error)
10117356Skarels 		return;
10228829Skarels 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
10328829Skarels 	if (timedelta == 0)
10428829Skarels 		if (ndelta > bigadj)
10528829Skarels 			tickdelta = 10 * tickadj;
10628829Skarels 		else
10728829Skarels 			tickdelta = tickadj;
10828829Skarels 	if (ndelta % tickdelta)
10928829Skarels 		ndelta = ndelta / tickadj * tickadj;
11028829Skarels 
11125170Skarels 	s = splclock();
11217356Skarels 	if (uap->olddelta) {
11328829Skarels 		oatv.tv_sec = timedelta / 1000000;
11428829Skarels 		oatv.tv_usec = timedelta % 1000000;
11528829Skarels 	}
11628829Skarels 	timedelta = ndelta;
11728829Skarels 	splx(s);
11828829Skarels 
11928829Skarels 	if (uap->olddelta)
12017356Skarels 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
12117356Skarels 			sizeof (struct timeval));
12217356Skarels }
12317356Skarels 
1248146Sroot /*
1258146Sroot  * Get value of an interval timer.  The process virtual and
1268146Sroot  * profiling virtual time timers are kept in the u. area, since
1278146Sroot  * they can be swapped out.  These are kept internally in the
1288146Sroot  * way they are specified externally: in time until they expire.
1298146Sroot  *
1308146Sroot  * The real time interval timer is kept in the process table slot
1318146Sroot  * for the process, and its value (it_value) is kept as an
1328146Sroot  * absolute time rather than as a delta, so that it is easy to keep
1338146Sroot  * periodic real-time signals from drifting.
1348146Sroot  *
1358146Sroot  * Virtual time timers are processed in the hardclock() routine of
1368146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
1378146Sroot  * routine, called from the softclock() routine.  Since a callout
1388146Sroot  * may be delayed in real time due to interrupt processing in the system,
1398146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
1408146Sroot  * to be delayed in real time past when it is supposed to occur.  It
1418146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
1428146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
1438146Sroot  * absolute time the timer should go off.
1448146Sroot  */
1458034Sroot getitimer()
1468034Sroot {
1477424Sroot 	register struct a {
1488034Sroot 		u_int	which;
1498034Sroot 		struct	itimerval *itv;
1508034Sroot 	} *uap = (struct a *)u.u_ap;
1518114Sroot 	struct itimerval aitv;
1528034Sroot 	int s;
1537424Sroot 
1548034Sroot 	if (uap->which > 2) {
1558034Sroot 		u.u_error = EINVAL;
1568034Sroot 		return;
1577424Sroot 	}
15825897Skarels 	s = splclock();
1598114Sroot 	if (uap->which == ITIMER_REAL) {
1608146Sroot 		/*
1618146Sroot 		 * Convert from absoulte to relative time in .it_value
1628146Sroot 		 * part of real time timer.  If time for real time timer
1638146Sroot 		 * has passed return 0, else return difference between
1648146Sroot 		 * current time and time for the timer to go off.
1658146Sroot 		 */
1668114Sroot 		aitv = u.u_procp->p_realtimer;
1678114Sroot 		if (timerisset(&aitv.it_value))
1688114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1698114Sroot 				timerclear(&aitv.it_value);
1708114Sroot 			else
1718114Sroot 				timevalsub(&aitv.it_value, &time);
1728114Sroot 	} else
1738114Sroot 		aitv = u.u_timer[uap->which];
1748114Sroot 	splx(s);
1759998Ssam 	u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
1769998Ssam 	    sizeof (struct itimerval));
1778034Sroot 	splx(s);
1787424Sroot }
1797424Sroot 
1808034Sroot setitimer()
1817424Sroot {
1827424Sroot 	register struct a {
1838034Sroot 		u_int	which;
1848103Sroot 		struct	itimerval *itv, *oitv;
1858034Sroot 	} *uap = (struct a *)u.u_ap;
18625227Smckusick 	struct itimerval aitv, *aitvp;
1878034Sroot 	int s;
1888114Sroot 	register struct proc *p = u.u_procp;
1897424Sroot 
1908034Sroot 	if (uap->which > 2) {
1918034Sroot 		u.u_error = EINVAL;
1928103Sroot 		return;
1937424Sroot 	}
19425227Smckusick 	aitvp = uap->itv;
1958103Sroot 	if (uap->oitv) {
1968103Sroot 		uap->itv = uap->oitv;
1978103Sroot 		getitimer();
1988103Sroot 	}
19925227Smckusick 	if (aitvp == 0)
20025227Smckusick 		return;
20126355Skarels 	u.u_error = copyin((caddr_t)aitvp, (caddr_t)&aitv,
20226355Skarels 	    sizeof (struct itimerval));
20325227Smckusick 	if (u.u_error)
20425227Smckusick 		return;
2058103Sroot 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) {
2068103Sroot 		u.u_error = EINVAL;
2078103Sroot 		return;
2088103Sroot 	}
20925897Skarels 	s = splclock();
2108114Sroot 	if (uap->which == ITIMER_REAL) {
2118625Sroot 		untimeout(realitexpire, (caddr_t)p);
2128114Sroot 		if (timerisset(&aitv.it_value)) {
2138114Sroot 			timevaladd(&aitv.it_value, &time);
2148625Sroot 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
2158114Sroot 		}
2168114Sroot 		p->p_realtimer = aitv;
2178114Sroot 	} else
2188103Sroot 		u.u_timer[uap->which] = aitv;
2198034Sroot 	splx(s);
2207424Sroot }
2217424Sroot 
2228146Sroot /*
2238146Sroot  * Real interval timer expired:
2248146Sroot  * send process whose timer expired an alarm signal.
2258146Sroot  * If time is not set up to reload, then just return.
2268146Sroot  * Else compute next time timer should go off which is > current time.
2278146Sroot  * This is where delay in processing this timeout causes multiple
2288146Sroot  * SIGALRM calls to be compressed into one.
2298146Sroot  */
2308146Sroot realitexpire(p)
2318114Sroot 	register struct proc *p;
2328114Sroot {
2338114Sroot 	int s;
2348114Sroot 
2358114Sroot 	psignal(p, SIGALRM);
2368114Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
2378114Sroot 		timerclear(&p->p_realtimer.it_value);
2388114Sroot 		return;
2398114Sroot 	}
2408114Sroot 	for (;;) {
24125897Skarels 		s = splclock();
2428114Sroot 		timevaladd(&p->p_realtimer.it_value,
2438114Sroot 		    &p->p_realtimer.it_interval);
2448114Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
2458625Sroot 			timeout(realitexpire, (caddr_t)p,
2468625Sroot 			    hzto(&p->p_realtimer.it_value));
2478114Sroot 			splx(s);
2488114Sroot 			return;
2498114Sroot 		}
2508114Sroot 		splx(s);
2518114Sroot 	}
2528114Sroot }
2538114Sroot 
2548146Sroot /*
2558146Sroot  * Check that a proposed value to load into the .it_value or
2568146Sroot  * .it_interval part of an interval timer is acceptable, and
2578146Sroot  * fix it to have at least minimal value (i.e. if it is less
2588146Sroot  * than the resolution of the clock, round it up.)
2598146Sroot  */
2608103Sroot itimerfix(tv)
2618103Sroot 	struct timeval *tv;
2627424Sroot {
2638034Sroot 
2648114Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2658114Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
2668103Sroot 		return (EINVAL);
26712970Ssam 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
2688103Sroot 		tv->tv_usec = tick;
2698103Sroot 	return (0);
2708034Sroot }
2718034Sroot 
2728146Sroot /*
2738146Sroot  * Decrement an interval timer by a specified number
2748146Sroot  * of microseconds, which must be less than a second,
2758146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2768146Sroot  * it.  In this case, carry over (usec - old value) to
2778146Sroot  * reducint the value reloaded into the timer so that
2788146Sroot  * the timer does not drift.  This routine assumes
2798146Sroot  * that it is called in a context where the timers
2808146Sroot  * on which it is operating cannot change in value.
2818146Sroot  */
2828034Sroot itimerdecr(itp, usec)
2838034Sroot 	register struct itimerval *itp;
2848034Sroot 	int usec;
2858034Sroot {
2868034Sroot 
2878103Sroot 	if (itp->it_value.tv_usec < usec) {
2888103Sroot 		if (itp->it_value.tv_sec == 0) {
2898146Sroot 			/* expired, and already in next interval */
2908103Sroot 			usec -= itp->it_value.tv_usec;
2918034Sroot 			goto expire;
2928103Sroot 		}
2938103Sroot 		itp->it_value.tv_usec += 1000000;
2948103Sroot 		itp->it_value.tv_sec--;
2958034Sroot 	}
2968103Sroot 	itp->it_value.tv_usec -= usec;
2978103Sroot 	usec = 0;
2988103Sroot 	if (timerisset(&itp->it_value))
2998034Sroot 		return (1);
3008146Sroot 	/* expired, exactly at end of interval */
3018034Sroot expire:
3028103Sroot 	if (timerisset(&itp->it_interval)) {
3038103Sroot 		itp->it_value = itp->it_interval;
3048103Sroot 		itp->it_value.tv_usec -= usec;
3058103Sroot 		if (itp->it_value.tv_usec < 0) {
3068103Sroot 			itp->it_value.tv_usec += 1000000;
3078103Sroot 			itp->it_value.tv_sec--;
3088103Sroot 		}
3098103Sroot 	} else
3108146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
3118034Sroot 	return (0);
3128034Sroot }
3138034Sroot 
3148146Sroot /*
3158146Sroot  * Add and subtract routines for timevals.
3168146Sroot  * N.B.: subtract routine doesn't deal with
3178146Sroot  * results which are before the beginning,
3188146Sroot  * it just gets very confused in this case.
3198146Sroot  * Caveat emptor.
3208146Sroot  */
3218146Sroot timevaladd(t1, t2)
3228146Sroot 	struct timeval *t1, *t2;
3238146Sroot {
3248146Sroot 
3258146Sroot 	t1->tv_sec += t2->tv_sec;
3268146Sroot 	t1->tv_usec += t2->tv_usec;
3278146Sroot 	timevalfix(t1);
3288146Sroot }
3298146Sroot 
3308146Sroot timevalsub(t1, t2)
3318146Sroot 	struct timeval *t1, *t2;
3328146Sroot {
3338146Sroot 
3348146Sroot 	t1->tv_sec -= t2->tv_sec;
3358146Sroot 	t1->tv_usec -= t2->tv_usec;
3368146Sroot 	timevalfix(t1);
3378146Sroot }
3388146Sroot 
3398146Sroot timevalfix(t1)
3408146Sroot 	struct timeval *t1;
3418146Sroot {
3428146Sroot 
3438146Sroot 	if (t1->tv_usec < 0) {
3448146Sroot 		t1->tv_sec--;
3458146Sroot 		t1->tv_usec += 1000000;
3468146Sroot 	}
3478146Sroot 	if (t1->tv_usec >= 1000000) {
3488146Sroot 		t1->tv_sec++;
3498146Sroot 		t1->tv_usec -= 1000000;
3508146Sroot 	}
3518146Sroot }
352