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