xref: /csrg-svn/sys/kern/kern_time.c (revision 44405)
123377Smckusick /*
237583Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337583Smckusick  * All rights reserved.
423377Smckusick  *
537583Smckusick  * Redistribution and use in source and binary forms are permitted
637583Smckusick  * provided that the above copyright notice and this paragraph are
737583Smckusick  * duplicated in all such forms and that any documentation,
837583Smckusick  * advertising materials, and other materials related to such
937583Smckusick  * distribution and use acknowledge that the software was developed
1037583Smckusick  * by the University of California, Berkeley.  The name of the
1137583Smckusick  * University may not be used to endorse or promote products derived
1237583Smckusick  * from this software without specific prior written permission.
1337583Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437583Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537583Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637583Smckusick  *
17*44405Skarels  *	@(#)kern_time.c	7.12 (Berkeley) 06/28/90
1823377Smckusick  */
197424Sroot 
2017093Sbloom #include "param.h"
21*44405Skarels #include "user.h"
2217093Sbloom #include "kernel.h"
2317093Sbloom #include "proc.h"
247424Sroot 
2537520Smckusick #include "machine/reg.h"
2637520Smckusick #include "machine/cpu.h"
2729946Skarels 
288103Sroot /*
298103Sroot  * Time of day and interval timer support.
308146Sroot  *
318146Sroot  * These routines provide the kernel entry points to get and set
328146Sroot  * the time-of-day and per-process interval timers.  Subroutines
338146Sroot  * here provide support for adding and subtracting timeval structures
348146Sroot  * and decrementing interval timers, optionally reloading the interval
358146Sroot  * timers when they expire.
368103Sroot  */
378103Sroot 
3843392Skarels /* ARGSUSED */
3943392Skarels gettimeofday(p, uap, retval)
4043392Skarels 	struct proc *p;
4143392Skarels 	register struct args {
428034Sroot 		struct	timeval *tp;
438034Sroot 		struct	timezone *tzp;
4443392Skarels 	} *uap;
4543392Skarels 	int *retval;
4643392Skarels {
478034Sroot 	struct timeval atv;
4843392Skarels 	int error = 0;
497500Sroot 
5030666Sbostic 	if (uap->tp) {
5130666Sbostic 		microtime(&atv);
5243392Skarels 		if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
5343392Skarels 		    sizeof (atv)))
54*44405Skarels 			return (error);
5530666Sbostic 	}
5630666Sbostic 	if (uap->tzp)
5743392Skarels 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
5843392Skarels 		    sizeof (tz));
59*44405Skarels 	return (error);
607500Sroot }
617500Sroot 
6243392Skarels settimeofday(p, uap, retval)
6343392Skarels 	struct proc *p;
6443392Skarels 	struct args {
658103Sroot 		struct	timeval *tv;
668103Sroot 		struct	timezone *tzp;
6743392Skarels 	} *uap;
6843392Skarels 	int *retval;
6943392Skarels {
708034Sroot 	struct timeval atv;
718034Sroot 	struct timezone atz;
7243392Skarels 	int error, s;
737500Sroot 
7443392Skarels 	if (error = suser(u.u_cred, &u.u_acflag))
75*44405Skarels 		return (error);
7630666Sbostic 	if (uap->tv) {
7743392Skarels 		if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
7843392Skarels 		    sizeof (struct timeval)))
79*44405Skarels 			return (error);
8037583Smckusick 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
8137583Smckusick 		boottime.tv_sec += atv.tv_sec - time.tv_sec;
8237583Smckusick 		s = splhigh(); time = atv; splx(s);
8337583Smckusick 		resettodr();
8430666Sbostic 	}
8543392Skarels 	if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
8643392Skarels 	    sizeof (atz))) == 0)
8737591Smckusick 		tz = atz;
88*44405Skarels 	return (error);
897500Sroot }
907500Sroot 
9128829Skarels extern	int tickadj;			/* "standard" clock skew, us./tick */
9228829Skarels int	tickdelta;			/* current clock skew, us. per tick */
9328829Skarels long	timedelta;			/* unapplied time correction, us. */
9428829Skarels long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
9517356Skarels 
9643392Skarels /* ARGSUSED */
9743392Skarels adjtime(p, uap, retval)
9843392Skarels 	struct proc *p;
9943392Skarels 	register struct args {
10017356Skarels 		struct timeval *delta;
10117356Skarels 		struct timeval *olddelta;
10243392Skarels 	} *uap;
10343392Skarels 	int *retval;
10443392Skarels {
10517356Skarels 	struct timeval atv, oatv;
10628829Skarels 	register long ndelta;
10743392Skarels 	int s, error;
10817356Skarels 
10943392Skarels 	if (error = suser(u.u_cred, &u.u_acflag))
110*44405Skarels 		return (error);
11143392Skarels 	if (error =
11243392Skarels 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
113*44405Skarels 		return (error);
11428829Skarels 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
11528829Skarels 	if (timedelta == 0)
11628829Skarels 		if (ndelta > bigadj)
11728829Skarels 			tickdelta = 10 * tickadj;
11828829Skarels 		else
11928829Skarels 			tickdelta = tickadj;
12028829Skarels 	if (ndelta % tickdelta)
12128829Skarels 		ndelta = ndelta / tickadj * tickadj;
12228829Skarels 
12325170Skarels 	s = splclock();
12417356Skarels 	if (uap->olddelta) {
12528829Skarels 		oatv.tv_sec = timedelta / 1000000;
12628829Skarels 		oatv.tv_usec = timedelta % 1000000;
12728829Skarels 	}
12828829Skarels 	timedelta = ndelta;
12928829Skarels 	splx(s);
13028829Skarels 
13128829Skarels 	if (uap->olddelta)
13217356Skarels 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
13317356Skarels 			sizeof (struct timeval));
134*44405Skarels 	return (0);
13517356Skarels }
13617356Skarels 
1378146Sroot /*
1388146Sroot  * Get value of an interval timer.  The process virtual and
1398146Sroot  * profiling virtual time timers are kept in the u. area, since
1408146Sroot  * they can be swapped out.  These are kept internally in the
1418146Sroot  * way they are specified externally: in time until they expire.
1428146Sroot  *
1438146Sroot  * The real time interval timer is kept in the process table slot
1448146Sroot  * for the process, and its value (it_value) is kept as an
1458146Sroot  * absolute time rather than as a delta, so that it is easy to keep
1468146Sroot  * periodic real-time signals from drifting.
1478146Sroot  *
1488146Sroot  * Virtual time timers are processed in the hardclock() routine of
1498146Sroot  * kern_clock.c.  The real time timer is processed by a timeout
1508146Sroot  * routine, called from the softclock() routine.  Since a callout
1518146Sroot  * may be delayed in real time due to interrupt processing in the system,
1528146Sroot  * it is possible for the real time timeout routine (realitexpire, given below),
1538146Sroot  * to be delayed in real time past when it is supposed to occur.  It
1548146Sroot  * does not suffice, therefore, to reload the real timer .it_value from the
1558146Sroot  * real time timers .it_interval.  Rather, we compute the next time in
1568146Sroot  * absolute time the timer should go off.
1578146Sroot  */
15843392Skarels /* ARGSUSED */
15943392Skarels getitimer(p, uap, retval)
16043392Skarels 	struct proc *p;
16143392Skarels 	register struct args {
1628034Sroot 		u_int	which;
1638034Sroot 		struct	itimerval *itv;
16443392Skarels 	} *uap;
16543392Skarels 	int *retval;
16643392Skarels {
1678114Sroot 	struct itimerval aitv;
1688034Sroot 	int s;
1697424Sroot 
17043392Skarels 	if (uap->which > ITIMER_PROF)
171*44405Skarels 		return (EINVAL);
17225897Skarels 	s = splclock();
1738114Sroot 	if (uap->which == ITIMER_REAL) {
1748146Sroot 		/*
1758146Sroot 		 * Convert from absoulte to relative time in .it_value
1768146Sroot 		 * part of real time timer.  If time for real time timer
1778146Sroot 		 * has passed return 0, else return difference between
1788146Sroot 		 * current time and time for the timer to go off.
1798146Sroot 		 */
18043392Skarels 		aitv = p->p_realtimer;
1818114Sroot 		if (timerisset(&aitv.it_value))
1828114Sroot 			if (timercmp(&aitv.it_value, &time, <))
1838114Sroot 				timerclear(&aitv.it_value);
1848114Sroot 			else
1858114Sroot 				timevalsub(&aitv.it_value, &time);
1868114Sroot 	} else
1878114Sroot 		aitv = u.u_timer[uap->which];
1888114Sroot 	splx(s);
189*44405Skarels 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
19043392Skarels 	    sizeof (struct itimerval)));
1917424Sroot }
1927424Sroot 
19343392Skarels /* ARGSUSED */
19443392Skarels setitimer(p, uap, retval)
19543392Skarels 	struct proc *p;
19643392Skarels 	register struct args {
1978034Sroot 		u_int	which;
1988103Sroot 		struct	itimerval *itv, *oitv;
19943392Skarels 	} *uap;
20043392Skarels 	int *retval;
20143392Skarels {
20237591Smckusick 	struct itimerval aitv;
20337591Smckusick 	register struct itimerval *itvp;
20443392Skarels 	int s, error;
2057424Sroot 
20643392Skarels 	if (uap->which > ITIMER_PROF)
207*44405Skarels 		return (EINVAL);
20837591Smckusick 	itvp = uap->itv;
20943392Skarels 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
21037591Smckusick 	    sizeof(struct itimerval))))
211*44405Skarels 		return (error);
21243392Skarels 	if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
213*44405Skarels 		return (error);
21437591Smckusick 	if (itvp == 0)
21543392Skarels 		return (0);
21643392Skarels 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
217*44405Skarels 		return (EINVAL);
21825897Skarels 	s = splclock();
2198114Sroot 	if (uap->which == ITIMER_REAL) {
2208625Sroot 		untimeout(realitexpire, (caddr_t)p);
2218114Sroot 		if (timerisset(&aitv.it_value)) {
2228114Sroot 			timevaladd(&aitv.it_value, &time);
2238625Sroot 			timeout(realitexpire, (caddr_t)p, hzto(&aitv.it_value));
2248114Sroot 		}
2258114Sroot 		p->p_realtimer = aitv;
2268114Sroot 	} else
2278103Sroot 		u.u_timer[uap->which] = aitv;
2288034Sroot 	splx(s);
229*44405Skarels 	return (0);
2307424Sroot }
2317424Sroot 
2328146Sroot /*
2338146Sroot  * Real interval timer expired:
2348146Sroot  * send process whose timer expired an alarm signal.
2358146Sroot  * If time is not set up to reload, then just return.
2368146Sroot  * Else compute next time timer should go off which is > current time.
2378146Sroot  * This is where delay in processing this timeout causes multiple
2388146Sroot  * SIGALRM calls to be compressed into one.
2398146Sroot  */
2408146Sroot realitexpire(p)
2418114Sroot 	register struct proc *p;
2428114Sroot {
2438114Sroot 	int s;
2448114Sroot 
2458114Sroot 	psignal(p, SIGALRM);
2468114Sroot 	if (!timerisset(&p->p_realtimer.it_interval)) {
2478114Sroot 		timerclear(&p->p_realtimer.it_value);
2488114Sroot 		return;
2498114Sroot 	}
2508114Sroot 	for (;;) {
25125897Skarels 		s = splclock();
2528114Sroot 		timevaladd(&p->p_realtimer.it_value,
2538114Sroot 		    &p->p_realtimer.it_interval);
2548114Sroot 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
2558625Sroot 			timeout(realitexpire, (caddr_t)p,
2568625Sroot 			    hzto(&p->p_realtimer.it_value));
2578114Sroot 			splx(s);
2588114Sroot 			return;
2598114Sroot 		}
2608114Sroot 		splx(s);
2618114Sroot 	}
2628114Sroot }
2638114Sroot 
2648146Sroot /*
2658146Sroot  * Check that a proposed value to load into the .it_value or
2668146Sroot  * .it_interval part of an interval timer is acceptable, and
2678146Sroot  * fix it to have at least minimal value (i.e. if it is less
2688146Sroot  * than the resolution of the clock, round it up.)
2698146Sroot  */
2708103Sroot itimerfix(tv)
2718103Sroot 	struct timeval *tv;
2727424Sroot {
2738034Sroot 
2748114Sroot 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
2758114Sroot 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
2768103Sroot 		return (EINVAL);
27712970Ssam 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
2788103Sroot 		tv->tv_usec = tick;
2798103Sroot 	return (0);
2808034Sroot }
2818034Sroot 
2828146Sroot /*
2838146Sroot  * Decrement an interval timer by a specified number
2848146Sroot  * of microseconds, which must be less than a second,
2858146Sroot  * i.e. < 1000000.  If the timer expires, then reload
2868146Sroot  * it.  In this case, carry over (usec - old value) to
2878146Sroot  * reducint the value reloaded into the timer so that
2888146Sroot  * the timer does not drift.  This routine assumes
2898146Sroot  * that it is called in a context where the timers
2908146Sroot  * on which it is operating cannot change in value.
2918146Sroot  */
2928034Sroot itimerdecr(itp, usec)
2938034Sroot 	register struct itimerval *itp;
2948034Sroot 	int usec;
2958034Sroot {
2968034Sroot 
2978103Sroot 	if (itp->it_value.tv_usec < usec) {
2988103Sroot 		if (itp->it_value.tv_sec == 0) {
2998146Sroot 			/* expired, and already in next interval */
3008103Sroot 			usec -= itp->it_value.tv_usec;
3018034Sroot 			goto expire;
3028103Sroot 		}
3038103Sroot 		itp->it_value.tv_usec += 1000000;
3048103Sroot 		itp->it_value.tv_sec--;
3058034Sroot 	}
3068103Sroot 	itp->it_value.tv_usec -= usec;
3078103Sroot 	usec = 0;
3088103Sroot 	if (timerisset(&itp->it_value))
3098034Sroot 		return (1);
3108146Sroot 	/* expired, exactly at end of interval */
3118034Sroot expire:
3128103Sroot 	if (timerisset(&itp->it_interval)) {
3138103Sroot 		itp->it_value = itp->it_interval;
3148103Sroot 		itp->it_value.tv_usec -= usec;
3158103Sroot 		if (itp->it_value.tv_usec < 0) {
3168103Sroot 			itp->it_value.tv_usec += 1000000;
3178103Sroot 			itp->it_value.tv_sec--;
3188103Sroot 		}
3198103Sroot 	} else
3208146Sroot 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
3218034Sroot 	return (0);
3228034Sroot }
3238034Sroot 
3248146Sroot /*
3258146Sroot  * Add and subtract routines for timevals.
3268146Sroot  * N.B.: subtract routine doesn't deal with
3278146Sroot  * results which are before the beginning,
3288146Sroot  * it just gets very confused in this case.
3298146Sroot  * Caveat emptor.
3308146Sroot  */
3318146Sroot timevaladd(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 timevalsub(t1, t2)
3418146Sroot 	struct timeval *t1, *t2;
3428146Sroot {
3438146Sroot 
3448146Sroot 	t1->tv_sec -= t2->tv_sec;
3458146Sroot 	t1->tv_usec -= t2->tv_usec;
3468146Sroot 	timevalfix(t1);
3478146Sroot }
3488146Sroot 
3498146Sroot timevalfix(t1)
3508146Sroot 	struct timeval *t1;
3518146Sroot {
3528146Sroot 
3538146Sroot 	if (t1->tv_usec < 0) {
3548146Sroot 		t1->tv_sec--;
3558146Sroot 		t1->tv_usec += 1000000;
3568146Sroot 	}
3578146Sroot 	if (t1->tv_usec >= 1000000) {
3588146Sroot 		t1->tv_sec++;
3598146Sroot 		t1->tv_usec -= 1000000;
3608146Sroot 	}
3618146Sroot }
362