xref: /csrg-svn/sys/kern/kern_time.c (revision 45120)
123377Smckusick /*
237583Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337583Smckusick  * All rights reserved.
423377Smckusick  *
544441Sbostic  * %sccs.include.redist.c%
637583Smckusick  *
7*45120Sbostic  *	@(#)kern_time.c	7.14 (Berkeley) 08/24/90
823377Smckusick  */
97424Sroot 
1017093Sbloom #include "param.h"
1144405Skarels #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 
2843392Skarels /* ARGSUSED */
2943392Skarels gettimeofday(p, uap, retval)
3043392Skarels 	struct proc *p;
3143392Skarels 	register struct args {
328034Sroot 		struct	timeval *tp;
338034Sroot 		struct	timezone *tzp;
3443392Skarels 	} *uap;
3543392Skarels 	int *retval;
3643392Skarels {
378034Sroot 	struct timeval atv;
3843392Skarels 	int error = 0;
397500Sroot 
4030666Sbostic 	if (uap->tp) {
4130666Sbostic 		microtime(&atv);
4243392Skarels 		if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
4343392Skarels 		    sizeof (atv)))
4444405Skarels 			return (error);
4530666Sbostic 	}
4630666Sbostic 	if (uap->tzp)
4743392Skarels 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
4843392Skarels 		    sizeof (tz));
4944405Skarels 	return (error);
507500Sroot }
517500Sroot 
52*45120Sbostic /* ARGSUSED */
5343392Skarels settimeofday(p, uap, retval)
5443392Skarels 	struct proc *p;
5543392Skarels 	struct args {
568103Sroot 		struct	timeval *tv;
578103Sroot 		struct	timezone *tzp;
5843392Skarels 	} *uap;
5943392Skarels 	int *retval;
6043392Skarels {
618034Sroot 	struct timeval atv;
628034Sroot 	struct timezone atz;
6343392Skarels 	int error, s;
647500Sroot 
6543392Skarels 	if (error = suser(u.u_cred, &u.u_acflag))
6644405Skarels 		return (error);
6730666Sbostic 	if (uap->tv) {
6843392Skarels 		if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
6943392Skarels 		    sizeof (struct timeval)))
7044405Skarels 			return (error);
7137583Smckusick 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
7237583Smckusick 		boottime.tv_sec += atv.tv_sec - time.tv_sec;
7337583Smckusick 		s = splhigh(); time = atv; splx(s);
7437583Smckusick 		resettodr();
7530666Sbostic 	}
7643392Skarels 	if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
7743392Skarels 	    sizeof (atz))) == 0)
7837591Smckusick 		tz = atz;
7944405Skarels 	return (error);
807500Sroot }
817500Sroot 
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 
8743392Skarels /* ARGSUSED */
8843392Skarels adjtime(p, uap, retval)
8943392Skarels 	struct proc *p;
9043392Skarels 	register struct args {
9117356Skarels 		struct timeval *delta;
9217356Skarels 		struct timeval *olddelta;
9343392Skarels 	} *uap;
9443392Skarels 	int *retval;
9543392Skarels {
9617356Skarels 	struct timeval atv, oatv;
9728829Skarels 	register long ndelta;
9843392Skarels 	int s, error;
9917356Skarels 
10043392Skarels 	if (error = suser(u.u_cred, &u.u_acflag))
10144405Skarels 		return (error);
10243392Skarels 	if (error =
10343392Skarels 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
10444405Skarels 		return (error);
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));
12544405Skarels 	return (0);
12617356Skarels }
12717356Skarels 
1288146Sroot /*
1298146Sroot  * Get value of an interval timer.  The process virtual and
1308146Sroot  * profiling virtual time timers are kept in the u. area, since
1318146Sroot  * they can be swapped out.  These are kept internally in the
1328146Sroot  * way they are specified externally: in time until they expire.
1338146Sroot  *
1348146Sroot  * The real time interval timer is kept in the process table slot
1358146Sroot  * for the process, and its value (it_value) is kept as an
1368146Sroot  * absolute time rather than as a delta, so that it is easy to keep
1378146Sroot  * periodic real-time signals from drifting.
1388146Sroot  *
1398146Sroot  * Virtual time timers are processed in the hardclock() routine of
1408146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
1418146Sroot  * routine, called from the softclock() routine.  Since a callout
1428146Sroot  * may be delayed in real time due to interrupt processing in the system,
1438146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
1448146Sroot  * to be delayed in real time past when it is supposed to occur.  It
1458146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
1468146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
1478146Sroot  * absolute time the timer should go off.
1488146Sroot  */
14943392Skarels /* ARGSUSED */
15043392Skarels getitimer(p, uap, retval)
15143392Skarels 	struct proc *p;
15243392Skarels 	register struct args {
1538034Sroot 		u_int	which;
1548034Sroot 		struct	itimerval *itv;
15543392Skarels 	} *uap;
15643392Skarels 	int *retval;
15743392Skarels {
1588114Sroot 	struct itimerval aitv;
1598034Sroot 	int s;
1607424Sroot 
16143392Skarels 	if (uap->which > ITIMER_PROF)
16244405Skarels 		return (EINVAL);
16325897Skarels 	s = splclock();
1648114Sroot 	if (uap->which == ITIMER_REAL) {
1658146Sroot 		/*
1668146Sroot 		 * Convert from absoulte to relative time in .it_value
1678146Sroot 		 * part of real time timer.  If time for real time timer
1688146Sroot 		 * has passed return 0, else return difference between
1698146Sroot 		 * current time and time for the timer to go off.
1708146Sroot 		 */
17143392Skarels 		aitv = p->p_realtimer;
1728114Sroot 		if (timerisset(&aitv.it_value))
1738114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1748114Sroot 				timerclear(&aitv.it_value);
1758114Sroot 			else
1768114Sroot 				timevalsub(&aitv.it_value, &time);
1778114Sroot 	} else
1788114Sroot 		aitv = u.u_timer[uap->which];
1798114Sroot 	splx(s);
18044405Skarels 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
18143392Skarels 	    sizeof (struct itimerval)));
1827424Sroot }
1837424Sroot 
18443392Skarels /* ARGSUSED */
18543392Skarels setitimer(p, uap, retval)
18643392Skarels 	struct proc *p;
18743392Skarels 	register struct args {
1888034Sroot 		u_int	which;
1898103Sroot 		struct	itimerval *itv, *oitv;
19043392Skarels 	} *uap;
19143392Skarels 	int *retval;
19243392Skarels {
19337591Smckusick 	struct itimerval aitv;
19437591Smckusick 	register struct itimerval *itvp;
19543392Skarels 	int s, error;
1967424Sroot 
19743392Skarels 	if (uap->which > ITIMER_PROF)
19844405Skarels 		return (EINVAL);
19937591Smckusick 	itvp = uap->itv;
20043392Skarels 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
20137591Smckusick 	    sizeof(struct itimerval))))
20244405Skarels 		return (error);
20343392Skarels 	if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
20444405Skarels 		return (error);
20537591Smckusick 	if (itvp == 0)
20643392Skarels 		return (0);
20743392Skarels 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
20844405Skarels 		return (EINVAL);
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);
22044405Skarels 	return (0);
2217424Sroot }
2227424Sroot 
2238146Sroot /*
2248146Sroot  * Real interval timer expired:
2258146Sroot  * send process whose timer expired an alarm signal.
2268146Sroot  * If time is not set up to reload, then just return.
2278146Sroot  * Else compute next time timer should go off which is > current time.
2288146Sroot  * This is where delay in processing this timeout causes multiple
2298146Sroot  * SIGALRM calls to be compressed into one.
2308146Sroot  */
2318146Sroot realitexpire(p)
2328114Sroot 	register struct proc *p;
2338114Sroot {
2348114Sroot 	int s;
2358114Sroot 
2368114Sroot 	psignal(p, SIGALRM);
2378114Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
2388114Sroot 		timerclear(&p->p_realtimer.it_value);
2398114Sroot 		return;
2408114Sroot 	}
2418114Sroot 	for (;;) {
24225897Skarels 		s = splclock();
2438114Sroot 		timevaladd(&p->p_realtimer.it_value,
2448114Sroot 		    &p->p_realtimer.it_interval);
2458114Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
2468625Sroot 			timeout(realitexpire, (caddr_t)p,
2478625Sroot 			    hzto(&p->p_realtimer.it_value));
2488114Sroot 			splx(s);
2498114Sroot 			return;
2508114Sroot 		}
2518114Sroot 		splx(s);
2528114Sroot 	}
2538114Sroot }
2548114Sroot 
2558146Sroot /*
2568146Sroot  * Check that a proposed value to load into the .it_value or
2578146Sroot  * .it_interval part of an interval timer is acceptable, and
2588146Sroot  * fix it to have at least minimal value (i.e. if it is less
2598146Sroot  * than the resolution of the clock, round it up.)
2608146Sroot  */
2618103Sroot itimerfix(tv)
2628103Sroot 	struct timeval *tv;
2637424Sroot {
2648034Sroot 
2658114Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2668114Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
2678103Sroot 		return (EINVAL);
26812970Ssam 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
2698103Sroot 		tv->tv_usec = tick;
2708103Sroot 	return (0);
2718034Sroot }
2728034Sroot 
2738146Sroot /*
2748146Sroot  * Decrement an interval timer by a specified number
2758146Sroot  * of microseconds, which must be less than a second,
2768146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2778146Sroot  * it.  In this case, carry over (usec - old value) to
2788146Sroot  * reducint the value reloaded into the timer so that
2798146Sroot  * the timer does not drift.  This routine assumes
2808146Sroot  * that it is called in a context where the timers
2818146Sroot  * on which it is operating cannot change in value.
2828146Sroot  */
2838034Sroot itimerdecr(itp, usec)
2848034Sroot 	register struct itimerval *itp;
2858034Sroot 	int usec;
2868034Sroot {
2878034Sroot 
2888103Sroot 	if (itp->it_value.tv_usec < usec) {
2898103Sroot 		if (itp->it_value.tv_sec == 0) {
2908146Sroot 			/* expired, and already in next interval */
2918103Sroot 			usec -= itp->it_value.tv_usec;
2928034Sroot 			goto expire;
2938103Sroot 		}
2948103Sroot 		itp->it_value.tv_usec += 1000000;
2958103Sroot 		itp->it_value.tv_sec--;
2968034Sroot 	}
2978103Sroot 	itp->it_value.tv_usec -= usec;
2988103Sroot 	usec = 0;
2998103Sroot 	if (timerisset(&itp->it_value))
3008034Sroot 		return (1);
3018146Sroot 	/* expired, exactly at end of interval */
3028034Sroot expire:
3038103Sroot 	if (timerisset(&itp->it_interval)) {
3048103Sroot 		itp->it_value = itp->it_interval;
3058103Sroot 		itp->it_value.tv_usec -= usec;
3068103Sroot 		if (itp->it_value.tv_usec < 0) {
3078103Sroot 			itp->it_value.tv_usec += 1000000;
3088103Sroot 			itp->it_value.tv_sec--;
3098103Sroot 		}
3108103Sroot 	} else
3118146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
3128034Sroot 	return (0);
3138034Sroot }
3148034Sroot 
3158146Sroot /*
3168146Sroot  * Add and subtract routines for timevals.
3178146Sroot  * N.B.: subtract routine doesn't deal with
3188146Sroot  * results which are before the beginning,
3198146Sroot  * it just gets very confused in this case.
3208146Sroot  * Caveat emptor.
3218146Sroot  */
3228146Sroot timevaladd(t1, t2)
3238146Sroot 	struct timeval *t1, *t2;
3248146Sroot {
3258146Sroot 
3268146Sroot 	t1->tv_sec += t2->tv_sec;
3278146Sroot 	t1->tv_usec += t2->tv_usec;
3288146Sroot 	timevalfix(t1);
3298146Sroot }
3308146Sroot 
3318146Sroot timevalsub(t1, t2)
3328146Sroot 	struct timeval *t1, *t2;
3338146Sroot {
3348146Sroot 
3358146Sroot 	t1->tv_sec -= t2->tv_sec;
3368146Sroot 	t1->tv_usec -= t2->tv_usec;
3378146Sroot 	timevalfix(t1);
3388146Sroot }
3398146Sroot 
3408146Sroot timevalfix(t1)
3418146Sroot 	struct timeval *t1;
3428146Sroot {
3438146Sroot 
3448146Sroot 	if (t1->tv_usec < 0) {
3458146Sroot 		t1->tv_sec--;
3468146Sroot 		t1->tv_usec += 1000000;
3478146Sroot 	}
3488146Sroot 	if (t1->tv_usec >= 1000000) {
3498146Sroot 		t1->tv_sec++;
3508146Sroot 		t1->tv_usec -= 1000000;
3518146Sroot 	}
3528146Sroot }
353