xref: /csrg-svn/sys/kern/kern_time.c (revision 37553)
123377Smckusick /*
229097Smckusick  * 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*37553Smckusick  *	@(#)kern_time.c	7.8 (Berkeley) 04/26/89
723377Smckusick  */
87424Sroot 
917093Sbloom #include "param.h"
1017093Sbloom #include "dir.h"		/* XXX */
1117093Sbloom #include "user.h"
1217093Sbloom #include "kernel.h"
1317093Sbloom #include "proc.h"
147424Sroot 
1537520Smckusick #include "machine/reg.h"
1637520Smckusick #include "machine/cpu.h"
1729946Skarels 
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 
3630666Sbostic 	if (uap->tp) {
3730666Sbostic 		microtime(&atv);
3830666Sbostic 		u.u_error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
3930666Sbostic 			sizeof (atv));
4030666Sbostic 		if (u.u_error)
4130666Sbostic 			return;
4230666Sbostic 	}
4330666Sbostic 	if (uap->tzp)
4430666Sbostic 		u.u_error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
4530666Sbostic 			sizeof (tz));
467500Sroot }
477500Sroot 
488034Sroot settimeofday()
497500Sroot {
508034Sroot 	register struct a {
518103Sroot 		struct	timeval *tv;
528103Sroot 		struct	timezone *tzp;
538034Sroot 	} *uap = (struct a *)u.u_ap;
548034Sroot 	struct timeval atv;
558034Sroot 	struct timezone atz;
567500Sroot 
5730666Sbostic 	if (uap->tv) {
5830666Sbostic 		u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
5930666Sbostic 			sizeof (struct timeval));
6030666Sbostic 		if (u.u_error)
6130666Sbostic 			return;
6230666Sbostic 		setthetime(&atv);
6330666Sbostic 	}
64*37553Smckusick 	if (uap->tzp == 0 || (u.u_error = suser(u.u_cred, &u.u_acflag)))
65*37553Smckusick 		return;
66*37553Smckusick 	u.u_error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof (atz));
67*37553Smckusick 	if (u.u_error == 0)
68*37553Smckusick 		tz = atz;
697500Sroot }
707500Sroot 
718103Sroot setthetime(tv)
728103Sroot 	struct timeval *tv;
738103Sroot {
748103Sroot 	int s;
758103Sroot 
76*37553Smckusick 	if (u.u_error = suser(u.u_cred, &u.u_acflag))
778103Sroot 		return;
788146Sroot /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
798103Sroot 	boottime.tv_sec += tv->tv_sec - time.tv_sec;
8025897Skarels 	s = splhigh(); time = *tv; splx(s);
819007Sroot 	resettodr();
828103Sroot }
838103Sroot 
8428829Skarels extern	int tickadj;			/* "standard" clock skew, us./tick */
8528829Skarels int	tickdelta;			/* current clock skew, us. per tick */
8628829Skarels long	timedelta;			/* unapplied time correction, us. */
8728829Skarels long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
8817356Skarels 
8917356Skarels adjtime()
9017356Skarels {
9117356Skarels 	register struct a {
9217356Skarels 		struct timeval *delta;
9317356Skarels 		struct timeval *olddelta;
9417356Skarels 	} *uap = (struct a *)u.u_ap;
9517356Skarels 	struct timeval atv, oatv;
9628829Skarels 	register long ndelta;
9725170Skarels 	int s;
9817356Skarels 
99*37553Smckusick 	if (u.u_error = suser(u.u_cred, &u.u_acflag))
10017356Skarels 		return;
10117356Skarels 	u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv,
10217356Skarels 		sizeof (struct timeval));
10317356Skarels 	if (u.u_error)
10417356Skarels 		return;
10528829Skarels 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
10628829Skarels 	if (timedelta == 0)
10728829Skarels 		if (ndelta > bigadj)
10828829Skarels 			tickdelta = 10 * tickadj;
10928829Skarels 		else
11028829Skarels 			tickdelta = tickadj;
11128829Skarels 	if (ndelta % tickdelta)
11228829Skarels 		ndelta = ndelta / tickadj * tickadj;
11328829Skarels 
11425170Skarels 	s = splclock();
11517356Skarels 	if (uap->olddelta) {
11628829Skarels 		oatv.tv_sec = timedelta / 1000000;
11728829Skarels 		oatv.tv_usec = timedelta % 1000000;
11828829Skarels 	}
11928829Skarels 	timedelta = ndelta;
12028829Skarels 	splx(s);
12128829Skarels 
12228829Skarels 	if (uap->olddelta)
12317356Skarels 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
12417356Skarels 			sizeof (struct timeval));
12517356Skarels }
12617356Skarels 
1278146Sroot /*
1288146Sroot  * Get value of an interval timer.  The process virtual and
1298146Sroot  * profiling virtual time timers are kept in the u. area, since
1308146Sroot  * they can be swapped out.  These are kept internally in the
1318146Sroot  * way they are specified externally: in time until they expire.
1328146Sroot  *
1338146Sroot  * The real time interval timer is kept in the process table slot
1348146Sroot  * for the process, and its value (it_value) is kept as an
1358146Sroot  * absolute time rather than as a delta, so that it is easy to keep
1368146Sroot  * periodic real-time signals from drifting.
1378146Sroot  *
1388146Sroot  * Virtual time timers are processed in the hardclock() routine of
1398146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
1408146Sroot  * routine, called from the softclock() routine.  Since a callout
1418146Sroot  * may be delayed in real time due to interrupt processing in the system,
1428146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
1438146Sroot  * to be delayed in real time past when it is supposed to occur.  It
1448146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
1458146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
1468146Sroot  * absolute time the timer should go off.
1478146Sroot  */
1488034Sroot getitimer()
1498034Sroot {
1507424Sroot 	register struct a {
1518034Sroot 		u_int	which;
1528034Sroot 		struct	itimerval *itv;
1538034Sroot 	} *uap = (struct a *)u.u_ap;
1548114Sroot 	struct itimerval aitv;
1558034Sroot 	int s;
1567424Sroot 
15736939Sbostic 	if (uap->which > ITIMER_PROF) {
1588034Sroot 		u.u_error = EINVAL;
1598034Sroot 		return;
1607424Sroot 	}
16125897Skarels 	s = splclock();
1628114Sroot 	if (uap->which == ITIMER_REAL) {
1638146Sroot 		/*
1648146Sroot 		 * Convert from absoulte to relative time in .it_value
1658146Sroot 		 * part of real time timer.  If time for real time timer
1668146Sroot 		 * has passed return 0, else return difference between
1678146Sroot 		 * current time and time for the timer to go off.
1688146Sroot 		 */
1698114Sroot 		aitv = u.u_procp->p_realtimer;
1708114Sroot 		if (timerisset(&aitv.it_value))
1718114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1728114Sroot 				timerclear(&aitv.it_value);
1738114Sroot 			else
1748114Sroot 				timevalsub(&aitv.it_value, &time);
1758114Sroot 	} else
1768114Sroot 		aitv = u.u_timer[uap->which];
1778114Sroot 	splx(s);
1789998Ssam 	u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
1799998Ssam 	    sizeof (struct itimerval));
1807424Sroot }
1817424Sroot 
1828034Sroot setitimer()
1837424Sroot {
1847424Sroot 	register struct a {
1858034Sroot 		u_int	which;
1868103Sroot 		struct	itimerval *itv, *oitv;
1878034Sroot 	} *uap = (struct a *)u.u_ap;
18836939Sbostic 	struct itimerval aitv;
18936939Sbostic 	register struct itimerval *itvp;
1908034Sroot 	int s;
1918114Sroot 	register struct proc *p = u.u_procp;
1927424Sroot 
19336939Sbostic 	if (uap->which > ITIMER_PROF) {
1948034Sroot 		u.u_error = EINVAL;
1958103Sroot 		return;
1967424Sroot 	}
19736939Sbostic 	itvp = uap->itv;
19836939Sbostic 	if (itvp && (u.u_error = copyin((caddr_t)itvp, (caddr_t)&aitv,
19936939Sbostic 	    sizeof(struct itimerval))))
20036939Sbostic 		return;
20136939Sbostic 	if (uap->itv = uap->oitv) {
2028103Sroot 		getitimer();
20336939Sbostic 		if (u.u_error)
20436939Sbostic 			return;
2058103Sroot 	}
20636939Sbostic 	if (itvp == 0)
20725227Smckusick 		return;
2088103Sroot 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) {
2098103Sroot 		u.u_error = EINVAL;
2108103Sroot 		return;
2118103Sroot 	}
21225897Skarels 	s = splclock();
2138114Sroot 	if (uap->which == ITIMER_REAL) {
2148625Sroot 		untimeout(realitexpire, (caddr_t)p);
2158114Sroot 		if (timerisset(&aitv.it_value)) {
2168114Sroot 			timevaladd(&aitv.it_value, &time);
2178625Sroot 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
2188114Sroot 		}
2198114Sroot 		p->p_realtimer = aitv;
2208114Sroot 	} else
2218103Sroot 		u.u_timer[uap->which] = aitv;
2228034Sroot 	splx(s);
2237424Sroot }
2247424Sroot 
2258146Sroot /*
2268146Sroot  * Real interval timer expired:
2278146Sroot  * send process whose timer expired an alarm signal.
2288146Sroot  * If time is not set up to reload, then just return.
2298146Sroot  * Else compute next time timer should go off which is > current time.
2308146Sroot  * This is where delay in processing this timeout causes multiple
2318146Sroot  * SIGALRM calls to be compressed into one.
2328146Sroot  */
2338146Sroot realitexpire(p)
2348114Sroot 	register struct proc *p;
2358114Sroot {
2368114Sroot 	int s;
2378114Sroot 
2388114Sroot 	psignal(p, SIGALRM);
2398114Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
2408114Sroot 		timerclear(&p->p_realtimer.it_value);
2418114Sroot 		return;
2428114Sroot 	}
2438114Sroot 	for (;;) {
24425897Skarels 		s = splclock();
2458114Sroot 		timevaladd(&p->p_realtimer.it_value,
2468114Sroot 		    &p->p_realtimer.it_interval);
2478114Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
2488625Sroot 			timeout(realitexpire, (caddr_t)p,
2498625Sroot 			    hzto(&p->p_realtimer.it_value));
2508114Sroot 			splx(s);
2518114Sroot 			return;
2528114Sroot 		}
2538114Sroot 		splx(s);
2548114Sroot 	}
2558114Sroot }
2568114Sroot 
2578146Sroot /*
2588146Sroot  * Check that a proposed value to load into the .it_value or
2598146Sroot  * .it_interval part of an interval timer is acceptable, and
2608146Sroot  * fix it to have at least minimal value (i.e. if it is less
2618146Sroot  * than the resolution of the clock, round it up.)
2628146Sroot  */
2638103Sroot itimerfix(tv)
2648103Sroot 	struct timeval *tv;
2657424Sroot {
2668034Sroot 
2678114Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2688114Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
2698103Sroot 		return (EINVAL);
27012970Ssam 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
2718103Sroot 		tv->tv_usec = tick;
2728103Sroot 	return (0);
2738034Sroot }
2748034Sroot 
2758146Sroot /*
2768146Sroot  * Decrement an interval timer by a specified number
2778146Sroot  * of microseconds, which must be less than a second,
2788146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2798146Sroot  * it.  In this case, carry over (usec - old value) to
2808146Sroot  * reducint the value reloaded into the timer so that
2818146Sroot  * the timer does not drift.  This routine assumes
2828146Sroot  * that it is called in a context where the timers
2838146Sroot  * on which it is operating cannot change in value.
2848146Sroot  */
2858034Sroot itimerdecr(itp, usec)
2868034Sroot 	register struct itimerval *itp;
2878034Sroot 	int usec;
2888034Sroot {
2898034Sroot 
2908103Sroot 	if (itp->it_value.tv_usec < usec) {
2918103Sroot 		if (itp->it_value.tv_sec == 0) {
2928146Sroot 			/* expired, and already in next interval */
2938103Sroot 			usec -= itp->it_value.tv_usec;
2948034Sroot 			goto expire;
2958103Sroot 		}
2968103Sroot 		itp->it_value.tv_usec += 1000000;
2978103Sroot 		itp->it_value.tv_sec--;
2988034Sroot 	}
2998103Sroot 	itp->it_value.tv_usec -= usec;
3008103Sroot 	usec = 0;
3018103Sroot 	if (timerisset(&itp->it_value))
3028034Sroot 		return (1);
3038146Sroot 	/* expired, exactly at end of interval */
3048034Sroot expire:
3058103Sroot 	if (timerisset(&itp->it_interval)) {
3068103Sroot 		itp->it_value = itp->it_interval;
3078103Sroot 		itp->it_value.tv_usec -= usec;
3088103Sroot 		if (itp->it_value.tv_usec < 0) {
3098103Sroot 			itp->it_value.tv_usec += 1000000;
3108103Sroot 			itp->it_value.tv_sec--;
3118103Sroot 		}
3128103Sroot 	} else
3138146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
3148034Sroot 	return (0);
3158034Sroot }
3168034Sroot 
3178146Sroot /*
3188146Sroot  * Add and subtract routines for timevals.
3198146Sroot  * N.B.: subtract routine doesn't deal with
3208146Sroot  * results which are before the beginning,
3218146Sroot  * it just gets very confused in this case.
3228146Sroot  * Caveat emptor.
3238146Sroot  */
3248146Sroot timevaladd(t1, t2)
3258146Sroot 	struct timeval *t1, *t2;
3268146Sroot {
3278146Sroot 
3288146Sroot 	t1->tv_sec += t2->tv_sec;
3298146Sroot 	t1->tv_usec += t2->tv_usec;
3308146Sroot 	timevalfix(t1);
3318146Sroot }
3328146Sroot 
3338146Sroot timevalsub(t1, t2)
3348146Sroot 	struct timeval *t1, *t2;
3358146Sroot {
3368146Sroot 
3378146Sroot 	t1->tv_sec -= t2->tv_sec;
3388146Sroot 	t1->tv_usec -= t2->tv_usec;
3398146Sroot 	timevalfix(t1);
3408146Sroot }
3418146Sroot 
3428146Sroot timevalfix(t1)
3438146Sroot 	struct timeval *t1;
3448146Sroot {
3458146Sroot 
3468146Sroot 	if (t1->tv_usec < 0) {
3478146Sroot 		t1->tv_sec--;
3488146Sroot 		t1->tv_usec += 1000000;
3498146Sroot 	}
3508146Sroot 	if (t1->tv_usec >= 1000000) {
3518146Sroot 		t1->tv_sec++;
3528146Sroot 		t1->tv_usec -= 1000000;
3538146Sroot 	}
3548146Sroot }
355