xref: /csrg-svn/sys/kern/kern_time.c (revision 29946)
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*29946Skarels  *	@(#)kern_time.c	7.3 (Berkeley) 11/03/86
723377Smckusick  */
87424Sroot 
917093Sbloom #include "param.h"
1017093Sbloom #include "dir.h"		/* XXX */
1117093Sbloom #include "user.h"
1217093Sbloom #include "kernel.h"
1317093Sbloom #include "inode.h"
1417093Sbloom #include "proc.h"
157424Sroot 
16*29946Skarels #include "../machine/reg.h"
17*29946Skarels #include "../machine/cpu.h"
18*29946Skarels 
198103Sroot /*
208103Sroot  * Time of day and interval timer support.
218146Sroot  *
228146Sroot  * These routines provide the kernel entry points to get and set
238146Sroot  * the time-of-day and per-process interval timers.  Subroutines
248146Sroot  * here provide support for adding and subtracting timeval structures
258146Sroot  * and decrementing interval timers, optionally reloading the interval
268146Sroot  * timers when they expire.
278103Sroot  */
288103Sroot 
298034Sroot gettimeofday()
307424Sroot {
318034Sroot 	register struct a {
328034Sroot 		struct	timeval *tp;
338034Sroot 		struct	timezone *tzp;
348034Sroot 	} *uap = (struct a *)u.u_ap;
358034Sroot 	struct timeval atv;
367500Sroot 
3725897Skarels 	microtime(&atv);
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;
7825897Skarels 	s = splhigh(); time = *tv; splx(s);
799007Sroot 	resettodr();
808103Sroot }
818103Sroot 
8228829Skarels extern	int tickadj;			/* "standard" clock skew, us./tick */
8328829Skarels int	tickdelta;			/* current clock skew, us. per tick */
8428829Skarels long	timedelta;			/* unapplied time correction, us. */
8528829Skarels long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
8617356Skarels 
8717356Skarels adjtime()
8817356Skarels {
8917356Skarels 	register struct a {
9017356Skarels 		struct timeval *delta;
9117356Skarels 		struct timeval *olddelta;
9217356Skarels 	} *uap = (struct a *)u.u_ap;
9317356Skarels 	struct timeval atv, oatv;
9428829Skarels 	register long ndelta;
9525170Skarels 	int s;
9617356Skarels 
9717356Skarels 	if (!suser())
9817356Skarels 		return;
9917356Skarels 	u.u_error = copyin((caddr_t)uap->delta, (caddr_t)&atv,
10017356Skarels 		sizeof (struct timeval));
10117356Skarels 	if (u.u_error)
10217356Skarels 		return;
10328829Skarels 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
10428829Skarels 	if (timedelta == 0)
10528829Skarels 		if (ndelta > bigadj)
10628829Skarels 			tickdelta = 10 * tickadj;
10728829Skarels 		else
10828829Skarels 			tickdelta = tickadj;
10928829Skarels 	if (ndelta % tickdelta)
11028829Skarels 		ndelta = ndelta / tickadj * tickadj;
11128829Skarels 
11225170Skarels 	s = splclock();
11317356Skarels 	if (uap->olddelta) {
11428829Skarels 		oatv.tv_sec = timedelta / 1000000;
11528829Skarels 		oatv.tv_usec = timedelta % 1000000;
11628829Skarels 	}
11728829Skarels 	timedelta = ndelta;
11828829Skarels 	splx(s);
11928829Skarels 
12028829Skarels 	if (uap->olddelta)
12117356Skarels 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
12217356Skarels 			sizeof (struct timeval));
12317356Skarels }
12417356Skarels 
1258146Sroot /*
1268146Sroot  * Get value of an interval timer.  The process virtual and
1278146Sroot  * profiling virtual time timers are kept in the u. area, since
1288146Sroot  * they can be swapped out.  These are kept internally in the
1298146Sroot  * way they are specified externally: in time until they expire.
1308146Sroot  *
1318146Sroot  * The real time interval timer is kept in the process table slot
1328146Sroot  * for the process, and its value (it_value) is kept as an
1338146Sroot  * absolute time rather than as a delta, so that it is easy to keep
1348146Sroot  * periodic real-time signals from drifting.
1358146Sroot  *
1368146Sroot  * Virtual time timers are processed in the hardclock() routine of
1378146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
1388146Sroot  * routine, called from the softclock() routine.  Since a callout
1398146Sroot  * may be delayed in real time due to interrupt processing in the system,
1408146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
1418146Sroot  * to be delayed in real time past when it is supposed to occur.  It
1428146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
1438146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
1448146Sroot  * absolute time the timer should go off.
1458146Sroot  */
1468034Sroot getitimer()
1478034Sroot {
1487424Sroot 	register struct a {
1498034Sroot 		u_int	which;
1508034Sroot 		struct	itimerval *itv;
1518034Sroot 	} *uap = (struct a *)u.u_ap;
1528114Sroot 	struct itimerval aitv;
1538034Sroot 	int s;
1547424Sroot 
1558034Sroot 	if (uap->which > 2) {
1568034Sroot 		u.u_error = EINVAL;
1578034Sroot 		return;
1587424Sroot 	}
15925897Skarels 	s = splclock();
1608114Sroot 	if (uap->which == ITIMER_REAL) {
1618146Sroot 		/*
1628146Sroot 		 * Convert from absoulte to relative time in .it_value
1638146Sroot 		 * part of real time timer.  If time for real time timer
1648146Sroot 		 * has passed return 0, else return difference between
1658146Sroot 		 * current time and time for the timer to go off.
1668146Sroot 		 */
1678114Sroot 		aitv = u.u_procp->p_realtimer;
1688114Sroot 		if (timerisset(&aitv.it_value))
1698114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1708114Sroot 				timerclear(&aitv.it_value);
1718114Sroot 			else
1728114Sroot 				timevalsub(&aitv.it_value, &time);
1738114Sroot 	} else
1748114Sroot 		aitv = u.u_timer[uap->which];
1758114Sroot 	splx(s);
1769998Ssam 	u.u_error = copyout((caddr_t)&aitv, (caddr_t)uap->itv,
1779998Ssam 	    sizeof (struct itimerval));
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