Home | History | Annotate | Line # | Download | only in kern
kern_time.c revision 1.4.4.1
      1      1.1      cgd /*
      2      1.1      cgd  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
      3      1.1      cgd  * All rights reserved.
      4      1.1      cgd  *
      5      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1      cgd  * modification, are permitted provided that the following conditions
      7      1.1      cgd  * are met:
      8      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1      cgd  *    must display the following acknowledgement:
     15      1.1      cgd  *	This product includes software developed by the University of
     16      1.1      cgd  *	California, Berkeley and its contributors.
     17      1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1      cgd  *    may be used to endorse or promote products derived from this software
     19      1.1      cgd  *    without specific prior written permission.
     20      1.1      cgd  *
     21      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1      cgd  * SUCH DAMAGE.
     32      1.1      cgd  *
     33      1.2      cgd  *	from: @(#)kern_time.c	7.15 (Berkeley) 3/17/91
     34  1.4.4.1  mycroft  *	$Id: kern_time.c,v 1.4.4.1 1993/11/14 20:32:14 mycroft Exp $
     35      1.1      cgd  */
     36      1.1      cgd 
     37  1.4.4.1  mycroft #include <sys/param.h>
     38  1.4.4.1  mycroft #include <sys/systm.h>
     39  1.4.4.1  mycroft #include <sys/resourcevar.h>
     40  1.4.4.1  mycroft #include <sys/kernel.h>
     41  1.4.4.1  mycroft #include <sys/proc.h>
     42      1.1      cgd 
     43  1.4.4.1  mycroft #include <machine/cpu.h>
     44      1.1      cgd 
     45      1.1      cgd /*
     46      1.1      cgd  * Time of day and interval timer support.
     47      1.1      cgd  *
     48      1.1      cgd  * These routines provide the kernel entry points to get and set
     49      1.1      cgd  * the time-of-day and per-process interval timers.  Subroutines
     50      1.1      cgd  * here provide support for adding and subtracting timeval structures
     51      1.1      cgd  * and decrementing interval timers, optionally reloading the interval
     52      1.1      cgd  * timers when they expire.
     53      1.1      cgd  */
     54      1.1      cgd 
     55      1.4      cgd struct gettimeofday_args {
     56      1.4      cgd 	struct	timeval *tp;
     57      1.4      cgd 	struct	timezone *tzp;
     58      1.4      cgd };
     59      1.4      cgd 
     60      1.1      cgd /* ARGSUSED */
     61      1.3   andrew int
     62      1.1      cgd gettimeofday(p, uap, retval)
     63      1.1      cgd 	struct proc *p;
     64      1.4      cgd 	register struct gettimeofday_args *uap;
     65      1.1      cgd 	int *retval;
     66      1.1      cgd {
     67      1.1      cgd 	struct timeval atv;
     68      1.1      cgd 	int error = 0;
     69      1.1      cgd 
     70      1.1      cgd 	if (uap->tp) {
     71      1.1      cgd 		microtime(&atv);
     72      1.1      cgd 		if (error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
     73      1.1      cgd 		    sizeof (atv)))
     74      1.1      cgd 			return (error);
     75      1.1      cgd 	}
     76      1.1      cgd 	if (uap->tzp)
     77      1.1      cgd 		error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
     78      1.1      cgd 		    sizeof (tz));
     79      1.1      cgd 	return (error);
     80      1.1      cgd }
     81      1.1      cgd 
     82      1.4      cgd struct settimeofday_args {
     83      1.4      cgd 	struct	timeval *tv;
     84      1.4      cgd 	struct	timezone *tzp;
     85      1.4      cgd };
     86      1.4      cgd 
     87      1.1      cgd /* ARGSUSED */
     88      1.3   andrew int
     89      1.1      cgd settimeofday(p, uap, retval)
     90      1.1      cgd 	struct proc *p;
     91      1.4      cgd 	struct settimeofday_args *uap;
     92      1.1      cgd 	int *retval;
     93      1.1      cgd {
     94      1.1      cgd 	struct timeval atv;
     95      1.1      cgd 	struct timezone atz;
     96      1.1      cgd 	int error, s;
     97      1.1      cgd 
     98      1.1      cgd 	if (error = suser(p->p_ucred, &p->p_acflag))
     99      1.1      cgd 		return (error);
    100      1.1      cgd 	if (uap->tv) {
    101      1.1      cgd 		if (error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
    102      1.1      cgd 		    sizeof (struct timeval)))
    103      1.1      cgd 			return (error);
    104      1.1      cgd 		/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
    105      1.1      cgd 		boottime.tv_sec += atv.tv_sec - time.tv_sec;
    106      1.1      cgd 		s = splhigh(); time = atv; splx(s);
    107      1.1      cgd 		resettodr();
    108      1.1      cgd 	}
    109      1.1      cgd 	if (uap->tzp && (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz,
    110      1.1      cgd 	    sizeof (atz))) == 0)
    111      1.1      cgd 		tz = atz;
    112      1.1      cgd 	return (error);
    113      1.1      cgd }
    114      1.1      cgd 
    115      1.1      cgd extern	int tickadj;			/* "standard" clock skew, us./tick */
    116      1.1      cgd int	tickdelta;			/* current clock skew, us. per tick */
    117      1.1      cgd long	timedelta;			/* unapplied time correction, us. */
    118      1.1      cgd long	bigadj = 1000000;		/* use 10x skew above bigadj us. */
    119      1.1      cgd 
    120      1.4      cgd struct adjtime_args {
    121      1.4      cgd 	struct timeval *delta;
    122      1.4      cgd 	struct timeval *olddelta;
    123      1.4      cgd };
    124      1.4      cgd 
    125      1.1      cgd /* ARGSUSED */
    126      1.3   andrew int
    127      1.1      cgd adjtime(p, uap, retval)
    128      1.1      cgd 	struct proc *p;
    129      1.4      cgd 	register struct adjtime_args *uap;
    130      1.1      cgd 	int *retval;
    131      1.1      cgd {
    132      1.1      cgd 	struct timeval atv, oatv;
    133      1.1      cgd 	register long ndelta;
    134      1.1      cgd 	int s, error;
    135      1.1      cgd 
    136      1.1      cgd 	if (error = suser(p->p_ucred, &p->p_acflag))
    137      1.1      cgd 		return (error);
    138      1.1      cgd 	if (error =
    139      1.1      cgd 	    copyin((caddr_t)uap->delta, (caddr_t)&atv, sizeof (struct timeval)))
    140      1.1      cgd 		return (error);
    141      1.1      cgd 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
    142      1.1      cgd 	if (timedelta == 0)
    143      1.1      cgd 		if (ndelta > bigadj)
    144      1.1      cgd 			tickdelta = 10 * tickadj;
    145      1.1      cgd 		else
    146      1.1      cgd 			tickdelta = tickadj;
    147      1.1      cgd 	if (ndelta % tickdelta)
    148      1.1      cgd 		ndelta = ndelta / tickadj * tickadj;
    149      1.1      cgd 
    150      1.1      cgd 	s = splclock();
    151      1.1      cgd 	if (uap->olddelta) {
    152      1.1      cgd 		oatv.tv_sec = timedelta / 1000000;
    153      1.1      cgd 		oatv.tv_usec = timedelta % 1000000;
    154      1.1      cgd 	}
    155      1.1      cgd 	timedelta = ndelta;
    156      1.1      cgd 	splx(s);
    157      1.1      cgd 
    158      1.1      cgd 	if (uap->olddelta)
    159      1.1      cgd 		(void) copyout((caddr_t)&oatv, (caddr_t)uap->olddelta,
    160      1.1      cgd 			sizeof (struct timeval));
    161      1.1      cgd 	return (0);
    162      1.1      cgd }
    163      1.1      cgd 
    164      1.1      cgd /*
    165      1.1      cgd  * Get value of an interval timer.  The process virtual and
    166      1.1      cgd  * profiling virtual time timers are kept in the p_stats area, since
    167      1.1      cgd  * they can be swapped out.  These are kept internally in the
    168      1.1      cgd  * way they are specified externally: in time until they expire.
    169      1.1      cgd  *
    170      1.1      cgd  * The real time interval timer is kept in the process table slot
    171      1.1      cgd  * for the process, and its value (it_value) is kept as an
    172      1.1      cgd  * absolute time rather than as a delta, so that it is easy to keep
    173      1.1      cgd  * periodic real-time signals from drifting.
    174      1.1      cgd  *
    175      1.1      cgd  * Virtual time timers are processed in the hardclock() routine of
    176      1.1      cgd  * kern_clock.c.  The real time timer is processed by a timeout
    177      1.1      cgd  * routine, called from the softclock() routine.  Since a callout
    178      1.1      cgd  * may be delayed in real time due to interrupt processing in the system,
    179      1.1      cgd  * it is possible for the real time timeout routine (realitexpire, given below),
    180      1.1      cgd  * to be delayed in real time past when it is supposed to occur.  It
    181      1.1      cgd  * does not suffice, therefore, to reload the real timer .it_value from the
    182      1.1      cgd  * real time timers .it_interval.  Rather, we compute the next time in
    183      1.1      cgd  * absolute time the timer should go off.
    184      1.1      cgd  */
    185      1.4      cgd 
    186      1.4      cgd struct getitimer_args {
    187      1.4      cgd 	u_int	which;
    188      1.4      cgd 	struct	itimerval *itv;
    189      1.4      cgd };
    190      1.4      cgd 
    191      1.1      cgd /* ARGSUSED */
    192      1.3   andrew int
    193      1.1      cgd getitimer(p, uap, retval)
    194      1.1      cgd 	struct proc *p;
    195      1.4      cgd 	register struct getitimer_args *uap;
    196      1.1      cgd 	int *retval;
    197      1.1      cgd {
    198      1.1      cgd 	struct itimerval aitv;
    199      1.1      cgd 	int s;
    200      1.1      cgd 
    201      1.1      cgd 	if (uap->which > ITIMER_PROF)
    202      1.1      cgd 		return (EINVAL);
    203      1.1      cgd 	s = splclock();
    204      1.1      cgd 	if (uap->which == ITIMER_REAL) {
    205      1.1      cgd 		/*
    206      1.1      cgd 		 * Convert from absoulte to relative time in .it_value
    207      1.1      cgd 		 * part of real time timer.  If time for real time timer
    208      1.1      cgd 		 * has passed return 0, else return difference between
    209      1.1      cgd 		 * current time and time for the timer to go off.
    210      1.1      cgd 		 */
    211      1.1      cgd 		aitv = p->p_realtimer;
    212      1.1      cgd 		if (timerisset(&aitv.it_value))
    213      1.1      cgd 			if (timercmp(&aitv.it_value, &time, <))
    214      1.1      cgd 				timerclear(&aitv.it_value);
    215      1.1      cgd 			else
    216      1.1      cgd 				timevalsub(&aitv.it_value, &time);
    217      1.1      cgd 	} else
    218      1.1      cgd 		aitv = p->p_stats->p_timer[uap->which];
    219      1.1      cgd 	splx(s);
    220      1.1      cgd 	return (copyout((caddr_t)&aitv, (caddr_t)uap->itv,
    221      1.1      cgd 	    sizeof (struct itimerval)));
    222      1.1      cgd }
    223      1.1      cgd 
    224      1.4      cgd struct setitimer_args {
    225      1.4      cgd 	u_int	which;
    226      1.4      cgd 	struct	itimerval *itv, *oitv;
    227      1.4      cgd };
    228      1.4      cgd 
    229      1.1      cgd /* ARGSUSED */
    230      1.3   andrew int
    231      1.1      cgd setitimer(p, uap, retval)
    232      1.1      cgd 	struct proc *p;
    233      1.4      cgd 	register struct setitimer_args *uap;
    234      1.1      cgd 	int *retval;
    235      1.1      cgd {
    236      1.1      cgd 	struct itimerval aitv;
    237      1.1      cgd 	register struct itimerval *itvp;
    238      1.1      cgd 	int s, error;
    239      1.1      cgd 
    240      1.1      cgd 	if (uap->which > ITIMER_PROF)
    241      1.1      cgd 		return (EINVAL);
    242      1.1      cgd 	itvp = uap->itv;
    243      1.1      cgd 	if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
    244      1.1      cgd 	    sizeof(struct itimerval))))
    245      1.1      cgd 		return (error);
    246      1.1      cgd 	if ((uap->itv = uap->oitv) && (error = getitimer(p, uap, retval)))
    247      1.1      cgd 		return (error);
    248      1.1      cgd 	if (itvp == 0)
    249      1.1      cgd 		return (0);
    250      1.1      cgd 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
    251      1.1      cgd 		return (EINVAL);
    252      1.1      cgd 	s = splclock();
    253      1.1      cgd 	if (uap->which == ITIMER_REAL) {
    254      1.3   andrew 		untimeout((timeout_t)realitexpire, (caddr_t)p);
    255      1.1      cgd 		if (timerisset(&aitv.it_value)) {
    256      1.1      cgd 			timevaladd(&aitv.it_value, &time);
    257      1.3   andrew 			timeout((timeout_t)realitexpire, (caddr_t)p, hzto(&aitv.it_value));
    258      1.1      cgd 		}
    259      1.1      cgd 		p->p_realtimer = aitv;
    260      1.1      cgd 	} else
    261      1.1      cgd 		p->p_stats->p_timer[uap->which] = aitv;
    262      1.1      cgd 	splx(s);
    263      1.1      cgd 	return (0);
    264      1.1      cgd }
    265      1.1      cgd 
    266      1.1      cgd /*
    267      1.1      cgd  * Real interval timer expired:
    268      1.1      cgd  * send process whose timer expired an alarm signal.
    269      1.1      cgd  * If time is not set up to reload, then just return.
    270      1.1      cgd  * Else compute next time timer should go off which is > current time.
    271      1.1      cgd  * This is where delay in processing this timeout causes multiple
    272      1.1      cgd  * SIGALRM calls to be compressed into one.
    273      1.1      cgd  */
    274      1.3   andrew void
    275      1.1      cgd realitexpire(p)
    276      1.1      cgd 	register struct proc *p;
    277      1.1      cgd {
    278      1.1      cgd 	int s;
    279      1.1      cgd 
    280      1.1      cgd 	psignal(p, SIGALRM);
    281      1.1      cgd 	if (!timerisset(&p->p_realtimer.it_interval)) {
    282      1.1      cgd 		timerclear(&p->p_realtimer.it_value);
    283      1.1      cgd 		return;
    284      1.1      cgd 	}
    285      1.1      cgd 	for (;;) {
    286      1.1      cgd 		s = splclock();
    287      1.1      cgd 		timevaladd(&p->p_realtimer.it_value,
    288      1.1      cgd 		    &p->p_realtimer.it_interval);
    289      1.1      cgd 		if (timercmp(&p->p_realtimer.it_value, &time, >)) {
    290      1.3   andrew 			timeout((timeout_t)realitexpire, (caddr_t)p,
    291      1.1      cgd 			    hzto(&p->p_realtimer.it_value));
    292      1.1      cgd 			splx(s);
    293      1.1      cgd 			return;
    294      1.1      cgd 		}
    295      1.1      cgd 		splx(s);
    296      1.1      cgd 	}
    297      1.1      cgd }
    298      1.1      cgd 
    299      1.1      cgd /*
    300      1.1      cgd  * Check that a proposed value to load into the .it_value or
    301      1.1      cgd  * .it_interval part of an interval timer is acceptable, and
    302      1.1      cgd  * fix it to have at least minimal value (i.e. if it is less
    303      1.1      cgd  * than the resolution of the clock, round it up.)
    304      1.1      cgd  */
    305      1.3   andrew int
    306      1.1      cgd itimerfix(tv)
    307      1.1      cgd 	struct timeval *tv;
    308      1.1      cgd {
    309      1.1      cgd 
    310      1.1      cgd 	if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
    311      1.1      cgd 	    tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    312      1.1      cgd 		return (EINVAL);
    313      1.1      cgd 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    314      1.1      cgd 		tv->tv_usec = tick;
    315      1.1      cgd 	return (0);
    316      1.1      cgd }
    317      1.1      cgd 
    318      1.1      cgd /*
    319      1.1      cgd  * Decrement an interval timer by a specified number
    320      1.1      cgd  * of microseconds, which must be less than a second,
    321      1.1      cgd  * i.e. < 1000000.  If the timer expires, then reload
    322      1.1      cgd  * it.  In this case, carry over (usec - old value) to
    323      1.1      cgd  * reducint the value reloaded into the timer so that
    324      1.1      cgd  * the timer does not drift.  This routine assumes
    325      1.1      cgd  * that it is called in a context where the timers
    326      1.1      cgd  * on which it is operating cannot change in value.
    327      1.1      cgd  */
    328      1.3   andrew int
    329      1.1      cgd itimerdecr(itp, usec)
    330      1.1      cgd 	register struct itimerval *itp;
    331      1.1      cgd 	int usec;
    332      1.1      cgd {
    333      1.1      cgd 
    334      1.1      cgd 	if (itp->it_value.tv_usec < usec) {
    335      1.1      cgd 		if (itp->it_value.tv_sec == 0) {
    336      1.1      cgd 			/* expired, and already in next interval */
    337      1.1      cgd 			usec -= itp->it_value.tv_usec;
    338      1.1      cgd 			goto expire;
    339      1.1      cgd 		}
    340      1.1      cgd 		itp->it_value.tv_usec += 1000000;
    341      1.1      cgd 		itp->it_value.tv_sec--;
    342      1.1      cgd 	}
    343      1.1      cgd 	itp->it_value.tv_usec -= usec;
    344      1.1      cgd 	usec = 0;
    345      1.1      cgd 	if (timerisset(&itp->it_value))
    346      1.1      cgd 		return (1);
    347      1.1      cgd 	/* expired, exactly at end of interval */
    348      1.1      cgd expire:
    349      1.1      cgd 	if (timerisset(&itp->it_interval)) {
    350      1.1      cgd 		itp->it_value = itp->it_interval;
    351      1.1      cgd 		itp->it_value.tv_usec -= usec;
    352      1.1      cgd 		if (itp->it_value.tv_usec < 0) {
    353      1.1      cgd 			itp->it_value.tv_usec += 1000000;
    354      1.1      cgd 			itp->it_value.tv_sec--;
    355      1.1      cgd 		}
    356      1.1      cgd 	} else
    357      1.1      cgd 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
    358      1.1      cgd 	return (0);
    359      1.1      cgd }
    360      1.1      cgd 
    361      1.1      cgd /*
    362      1.1      cgd  * Add and subtract routines for timevals.
    363      1.1      cgd  * N.B.: subtract routine doesn't deal with
    364      1.1      cgd  * results which are before the beginning,
    365      1.1      cgd  * it just gets very confused in this case.
    366      1.1      cgd  * Caveat emptor.
    367      1.1      cgd  */
    368      1.3   andrew void
    369      1.1      cgd timevaladd(t1, t2)
    370      1.1      cgd 	struct timeval *t1, *t2;
    371      1.1      cgd {
    372      1.1      cgd 
    373      1.1      cgd 	t1->tv_sec += t2->tv_sec;
    374      1.1      cgd 	t1->tv_usec += t2->tv_usec;
    375      1.1      cgd 	timevalfix(t1);
    376      1.1      cgd }
    377      1.1      cgd 
    378      1.3   andrew void
    379      1.1      cgd timevalsub(t1, t2)
    380      1.1      cgd 	struct timeval *t1, *t2;
    381      1.1      cgd {
    382      1.1      cgd 
    383      1.1      cgd 	t1->tv_sec -= t2->tv_sec;
    384      1.1      cgd 	t1->tv_usec -= t2->tv_usec;
    385      1.1      cgd 	timevalfix(t1);
    386      1.1      cgd }
    387      1.1      cgd 
    388      1.3   andrew void
    389      1.1      cgd timevalfix(t1)
    390      1.1      cgd 	struct timeval *t1;
    391      1.1      cgd {
    392      1.1      cgd 
    393      1.1      cgd 	if (t1->tv_usec < 0) {
    394      1.1      cgd 		t1->tv_sec--;
    395      1.1      cgd 		t1->tv_usec += 1000000;
    396      1.1      cgd 	}
    397      1.1      cgd 	if (t1->tv_usec >= 1000000) {
    398      1.1      cgd 		t1->tv_sec++;
    399      1.1      cgd 		t1->tv_usec -= 1000000;
    400      1.1      cgd 	}
    401      1.1      cgd }
    402